In this section we will take a look at lists and hashes. You will need these if you want to manipulate larger quantities of variables that are related, such as words, database records etc. Both lists and hashes are sets of scalar data. The difference between the two is that lists are ordered and hashes are unordered. Lists are always written with an at sign (@) before their name, instead of the usual dollar ($) for scalar variables. Hash names are preceded by a percentage sign (%). Remember that Perl will allow you to have completely unrelated variables with the same name, one might be a scalar (e.g. $something), the other a list (@something) and the third a hash (%something).
Lists are represented in your program by a comma-separated list enclosed in parentheses. The empty list is represented by an empty pair of parentheses. For example, the following expressions are lists:
@a = (); # empty list @b = (1,2,3); # three numbers @c = ("Jan","Piet","Marie"); # three strings @d = ("Dirk",1.92,46,"20-03-1977"); # a mixed list
In lists, variables are replaced by their values. Furthermore, Perl lists are flat, which means that they have only one dimension. When you include a list as a part of another list, the results will be a one-level list with elements of both lists mixed into one structure.
$a = 1; @b = ($a,$a+1,$a+2); # same as: (1,2,3) @c = ("Jan",("Piet","Marie")); # ("Jan","Piet","Marie") @d = ("Dirk",1.92,46,(),"20-03-1977"); # ("Dirk",1.92,46,"20-03-1977"); @e = (@b,@c); # (1,2,3,"Jan","Piet","Marie")
Note that in the third example, the empty list has disappeared.
There are several of practical construction operators for lists: The first operator is the qw() ("quote word") function, which allows you to list a bunch of strings without the quotes and comma's (only separated by whitespace), and make it into a list. For example: qw(Jan Piet Marie) is a shorter notation for ("Jan","Piet","Marie"). The second, a shorthand notation for a range of numbers or strings, is written as ($x..$y), and makes a list that consists of items ranging from $x to $y in steps of 1. For example:
@x = (1..6); # same as: (1,2,3,4,5,6) @y = ("a".."e"); # ("a","b","c","d","e") @z = ("AA".."CC"); # ("AA","BB","CC")
The elements of a list can be accessed via the subscripting operator "[]". Numbering starts at zero, so that $list[0] gives the first item in the list @list. Negative numbers count backwards from the last item in the list (i.e. $list[-1] refers to the last item). Note that we use a dollar sign because we are pointing to a single scalar value.
There are two ways to find out how many elements a (complete) list contains:
@list = ("an","bert","cindy","dirk"); $length = @list; # $length now has the value 4 $lastId = $#list; # $lastId has value 3 (ids start at 0)
You can also assign to lists (i.e. use lists of things on the left hand-side of an assignment). Examples:
($a,$b) = ("one","two"); # $a = "one" and $b = "two" ($one,@many) = (1,2,3,4,5,6); # $one = 1 and @many = (2,3,4,5,6) ($a,$b) = ($b,$a); # $a = "two" and $b = "one"
Assignment to a variable first evaluates the right hand-side of the expression, and then makes a copy of the result of that evaluation, including lists.
This section introduces a number of important functions for manipulating lists.
appends list2 to the end of the list1. If the second argument is a scalar rather than a list, it appends it as the last item of the list. The list grows as needed. For example:
@list1 = ("an","bert"); @list2 = ("cindy","dirk"); push(@list1,@list2); # @list1 is ("an","bert","cindy","dirk") push(@list2,"gerben"); # @list2 is ("cindy","dirk","gerben")
It removes an item from the end (right) of the list and returns it.
@list = ("an","bert","cindy"); $item = pop(@list); # $item is "cindy" and @list is ( "an","bert")
takes an item from the start (left) of the list but is otherwise the same as pop.
puts list elements or a single item on the start (left) of the list, just as push does for the right side.
lets you remove or replace entire ranges of items. splice removes the elements from pos to pos+number from the list and returns them in a list. If the LIST argument is provided, the removed range is replaced with the list. The list shrinks or grows as necessary.
@list = ("an","bert","cindy","dirk"); @cut = splice(@list,1,2); # @cut is ("bert","cindy") and @list is ("an","dirk") @list1 = ("an","bert","cindy","dirk"); @list2 = ("evelien","frank"); @cut = splice(@list1,1,2,@list2); # @cut is still ("bert","cindy") # but @list1 is now ("an","evelien","frank","dirk")
reverses the order of the elements of its argument, returning the resulting list. Note that it works on a copy and does not change the argument itself.
alphabetically sorts the elements of its argument, returning the resulting list. This function also works on a copy and does no harm to the original.
combines the elements of LIST in STRING1 with STRING2 (usually a single character) as separator. Example:
@list = ("an","bert","cindy"); $string = join(":",@list); # $string now has the value "an:bert:cindy"
divides STRING in several parts using REGEXP as separator and stores the parts in LIST. Examples:
$string = "an bert cindy"; @list = split(/\s+/,$string); # ("an","bert","cindy") $string = "::an::bert::cindy"; @list = split(/::/,$string); # ("","an","bert","cindy") $string = "an bert"; @list = split(//,$string); # ("a","n"," ","b","e","r","t")
Like lists, hashes are sets of objects. However, there are a few differences between lists and hashes:
A hash can be filled one element by one: $hash{"string"} = 123. It can also be filled with the contents of a list. In that case, all even elements of the list will become hash keys and all odd elements will become hash values:
$wordfrequency{"the"} = 12731; # creates key "the" with value 12731 %age = ("An","22","Bert","54"); # $age{"An"} = 22; $age{"Bert"} = 54; @list = %birthdays; # contents @list cannot be predicted
Note that conversion of a hash to a list is also possible (like in the last command) but that the contents of the list cannot be predicted because the hash is unordered.
There are a couple of built-in functions which operate on hashes:
In a program which works with hashes, you will often want to print a sorted version of a hash. This can be done with the following code:
foreach $key (sort keys %hash) { print "The value associated with key $key is $hash{$key}\n"; }
In this example, the keys of the hash are sorted and then the values associated with each key are printed. This action can be implemented in a more effective way with the function each() .