next up previous contents
Next: Control flow Up: Programming in Perl Previous: Scalar datatypes   Contents

Arrays

Perl has an array type, which is more like a list in Haskell because it can grow and shrink dynamically. However, unlike arrays in Java or C and lists in Haskell, Perl arrays can contain a mixture of numeric and string values. Collectively, a Perl array variable starts with the symbol @, thus distinguishing it from a scalar. An array can be explicitly written using the notation (element1,element2,...,elementk).

Perl supports array like notation to access individual elements of an array--array indices run from 0, as in C or Java. The element at position $i in array @list is referred to as $list[$i], not @list[$i]. A useful mnemonic is that an element of an array is a scalar, so we use $, not @.

The [...] notation can be used after the @ name of the array to denote a sublist. In this form, we provide a list of the indices to be picked out as a sublist. This list of indices can be in any order and can have repetitions.

  @list = ("zero",1,"two",3,4);  # Note the mixed types
  $val1 = $list[3];              # $val1 is now 3
  $list[4] = "four";             # @list is now ("zero",1,"two",3,"four")
  @list2 = @list[4];             # @list2 is ("four")
  @list3 = @list[4,1,3,4,0];     # @list3 is ("four",1,3,"four",0)

A key fact about Perl lists is that they are always flat--nested lists are automatically flattened out. Thus, we don't need to write functions such as append to combine lists.

  @list1 = (1,"two");
  @list2 = (3,4); 
  @list  = (@list1,@list2);      # @list is (1,"two",3,4)
  $list  = ("zero",@list);       # @list is now ("zero",1,"two",3,4)

The example above shows that we can use a list variable on the left hand side of an assignment. We can also combine scalar variables into a list to achieve a multiple parallel assignment. If we use a list variable as part of the multiple assignment on the left hand side it ``soaks'' up all the remaining values.

  ($first,$second) = ($list[0],$list[1]);
  ($first,@rest)   = @list;           # $first is $list[0]
                                      # @rest is @list[1,2,...]
  ($first,@rest,$last) = @list;       # $first is $list[0]
                                      # @rest is @list[1,2,...]
                                      # $last gets no value
  ($this,$that)    = ($that,$this);   # Swap two values!

Perl makes a distinction between list context and scalar context. The same expression often yields different values depending on the context in which it is placed. One example of this is that a list variable @list, when used in scalar context, evaluates to the length of the list.

  @n = @list;                   # Copies @list into @n
  $n = @list;                   # $n is the length of @list
  $m = $list[@list-1];          # $m is the last element of @list

Perl has several builtin functions to manipulate arrays. For instance, reverse @list reverses a list. The function shift @list removes and returns the leftmost element of @list. The function pop @list removes and returns the last element of @list. The function push(@list,$value) adds $value at the end of @list while unshift(@list,$value) adds $value at the beginning of @list.


next up previous contents
Next: Control flow Up: Programming in Perl Previous: Scalar datatypes   Contents
Madhavan Mukund 2004-04-29