String, Array and Hash Goodies

String Goodies

Quotes around strings can be single quotes, as in 'Hello World', or double, "Hello World". The difference is that double-quoted strings are extrapolated:

o

Escape-characters like \t, \n will only work in double-quoted strings. A string 'Hello World\n' will contain a literal backslash and n, not a newline character.

o

Interpolation also expands variables, as in:

 my $name = 'Jason';
 print("Hello there, $name.\n");

q-operators

Strings can be also initialized using the following special operators:

o

q/string/: a single-quoted string. The string can span several lines, as in:

 q/Copyright (c) 2014 ff. All rights reserved.
   For information, contact me@example.com./

Instead of a slash, you can use q(string) or q{string}, it's a matter of taste. You could even use qxstringx though that is confusing.

o

qq/string/: a double-quoted string (with extrapolation).

o

qw/string string string/: This automatically splits strings by whitespace and expands to an array of strings, as in:

 my @names = qw{Harry Hermione Fred George Ron};
 for my $n (@names) {
    print("$n\n");
 }

Exercise

What's the error in:

 qq/Copyright (c) 2014ff. All rights reserved.
    For information, contact me@example.com./

'Here-document'

Similar to shell scripting, Perl can indicate a string using << and a label, known as 'here-document':

 my $s = <<"END";
 This is a string over
 multiple lines.
 END

The ending label must be on its own line, at column 1, without anything following it.

The above example initializes an interpolated string (i.e., a "$whatever" in the string would be replaced with the value of a variable "$whatever"). Use single quotes to avoid interpolation.

Range Operator

A range is denoted by start..end and automatically expands into an array of numbers:

 for my $i (0..12) {
    print("$i\n");   # 0, 1, 2, .. 12
 }

Array size

scalar(@arr) returns the number of elements in an array. $#array returns the maximum usable index in the array:

 my @arr = ('a', 'b', 'c');
 print("There are ", scalar(@arr), " elements\n");

 for my $i (0..$#arr) {
    print("Element $i is $arr[$i]\n");
 }

Adding or removing array elements

o

Functions shift(@arr) and unshift(@arr) remove or add elements to the array start

o

Functions pop(@arr) and push(@arr) remove or add elements to the array end

o

To slice an array, use e.g.:

 @arr[3,4]     # new array with elements nrs 3 and 4
 @arr[3..10]   # new array with elements 3,4,5, etc until 10

Program Arguments

Program arguments are kept in a special array @ARGV. This array doesn't include the program name, $ARGV[0] is the first argument.

The program name is kept in $0.

Exercise

Write a program 'add.pl' that is invoked as follows:

 perl add.pl 3 4 5 6

It adds the numbers specified on the commandline and prints the result (in this case 18).

Hash operations

o

Keys of a hash: function

 keys(%myhash)

returns the keys, as in:

 for my $k (keys(%myhash)) {
   print("at key $k: value $myhash{$k}\n");
 }
o

Removing a key/value pair: function

 delete($myhash{$key})

removes a hash entry.

Exercise

Given the following hash:

 my %h = ( a => 1, b => 2, c => 3, d => 4);

Write a code snippet that prints how many keys the hash contains. Your code must be more sophisticated than print(4).