Previous | Home | Exercises | PDF slides | Next

 

Perl 2007: Lesson 4


This text is part of the lecture notes for a programming course taught at University of Tilburg, The Netherlands. The first version of this text was written by Jakub Zavrel.

4. Lists and hashes

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).

4.1. List basics

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.

4.2. List functions

This section introduces a number of important functions for manipulating lists.

4.3. Hash basics

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.

4.4. Hash functions

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() .


Previous | Home | Exercises | PDF slides | Next
Last update: October 03, 2007. erikt(at)science.uva.nl