Arrays (Lists)





                    CHAPTER 4: ARRAYS (LISTS)


   What Is An Array?

     - Ordered list of scalar data

     - Each array element is a separate scalar variable

     - Also called a list


   Array Literals

     - List of comma-separated scalar values

     - We place parentheses around the comma-separated values 
       when doing an assignment, since the precedence of the 
       comma operator is lower than that of the assignment operator.
       For this reason, array literals usually are enclosed in
       parentheses.

     - Ex.

         (10, 20, 30)              # List of numbers
         ("bob", "joe", "fred")    # List of strings
         (1, "bob", 2, "joe", 3)   # Both strings and numbers
         ($a - $b, $c, 22)         # Elements can be expressions

        
   List Constructor Operator

     - initial_value..final_value

     - Creates a list of values starting from initial_value up through 
       the final_value, incrementing by one each time

     - The list stops when the next value is greater than or equal to 
       the final_value

     - If the final_value is less than the initial_value, the empty list 
       is generated

     - Ex.

         (1..4)                    # Same as (1, 2, 3, 4)
         (1..3, 10)                # Same as (1, 2, 3, 10)
         (2.7..6.5)                # Same as (2.7, 3.7, 4.7, 5.7)
         (5..1)                    # Same as (), the empty list
         ($a..$b)                  # Range determined by current
                                   #   values of $a and $b


   Array Variables

     - Variable that holds a single array value

     - Name begins with a @

     - Array variables have their own namespace


   Array Assignment Operator (=)

     - Used to give a value to an array variable

     - Array variables have the empty list value before they are first 
       assigned

     - Perl determines whether the assignment is a scalar assignment or 
       an array assignment by the context

     - Ex.

         @names = ("bob", "joe");
         @words = @names;
         @x = 1;                   # @x is (1)
         @y = (8, @names, 10);     # @y is (8, "bob", "joe", 10)

     - Note that a list can NOT contain another list as an element.  ALL 
       list elements must be scalars.

     - An array assignment itself has a value.  This value is the list 
       value which is assigned to the array variable.

  
   Scalar And Array Context

     - If an operator expects an operand to be a scalar, then the operand 
       is being evaluated in a scalar context

     - If an operator expects an operand to be an array, then the operand 
       is being evaluated in an array context


   Special Array Assignments

     - If an array literal contains only variables (no expressions), the 
       array literal can be used as a variable on the left side of an 
       assignment

     - Each scalar variable in the array literal takes on the 
       corresponding value from the right side of the assignment

     - Ex.

         ($a, $b) = (1, 2);        # $a is 1, $b is 2
         ($a, $b) = ($b, $a);      # Swap $a and $b
         ($a, @b) = ($c, $d, $e);  # $a is $c, @b is ($d, $e)


   Array Length

     - Number of elements in the array

     - Returned if an array variable is used in a scalar context

     - Ex.

         @a = (5, 6, 7, 8);
         $x = @a;                  # $x is 4, the number of 
                                   #   elements in @a
         ($x) = @a;                # $x is 5, the first element 
                                   #   of @a

     - However, note that if we do

         $x = (5, 6, 7, 8);
      
       that $x is 8, since the comma-separated list (array literal)
       is evaluated in a context that does not require an array
       value for the right-hand side of the assignment.  Perl
       evaluates the right-hand side as an expression with several
       comma operators!

   
   Element Access

     - Array elements are numbered using sequential integers beginning 
       at 0

     - Array elements can be accessed using the subscript operator, []

     - Ex.

         @a = (5, 2, 7, 8);
         $b = $a[1];               # $b is 2
         $c = $a[$b];              # $c is 7
         $a[2] = -3;               # @a is now (5, 2, -3, 8)
         ($a[0], $a[1]) = ($a[1], $a[0]);  # Swap first two 
                                           #   elements of @a


   Slice

     - List of elements selected from an array

     - Slice subscript is itself a list 

     - Ex.

         @a[0,1]                   # Same as ($a[0], $a[1])
         @a[1, 3, 5]               # Same as ($a[1], $a[3],
                                   #   $a[5])

     - Ex.

         @x = (4, 5, 6);
         $x = @x[1,2];             # $x is 6.  The slice
                                   #   is treated as the literal
                                   #   (5, 6), and when assigned
                                   #   to a scalar yields 6!


   Beyond Array Boundaries

     - If you access an array element beyond the current length of the 
       array, the undef value is returned
  
     - If you assign a value to an array element beyond the current 
       length of the array, the array is automatically extended, with 
       the undef value given to all intermediate elements

     - Ex.

         @a = (5, 6, 7);
         $b = $a[4];               # $b is undef
         $a[4] = 9;                # @a is now (5, 6, 7, undef, 9)

     - If you access an array element with a subscript less than 0, the 
       undef value is returned

     - BUT if you attempt to assign a value to an array element with a 
       subscript less than 0, a fatal error occurs


   Value Of Subscript Of Last Element

     - Use $# notation

     - Ex.

         @a = (5, 6, 7);
         $b = $#a;                 # $b is 2


   Push/Pop Functions

     - Add or remove an element at the right-hand side of a list

     - Either a scalar or a list can be added

     - Ex.

         push (@a, $b);            # Same as @a = (@a, $b);
         @x = (1, 2);
         push (@a, @x);            # Same as @a = (@a, 1, 2);
         $c = pop (@a);            # Returns (and removes) last
                                   #   element of @a


   Unshift/Shift Functions

     - Add or remove an element at the left-hand side of a list

     - Either a scalar or a list can be added

     - Ex.

         unshift (@a, $b);         # Same as @a = ($b, @a);
         $c = shift (@a);          # Returns (and removes) first
                                   #   element of @a


   Other Functions

     - reverse (@a) : reverses the order of the elements of
                      the array @a

     - sort (@a)    : sorts the elements of the array @a in
                      ascending ASCII order

     - chop (@a)    : removes the last character from each
                      element of the array @a


   Array Variable Interpolation

     - Substitution of an array variable reference with its value 
       inside a double-quoted string literal

     - If an array or array slice is included in a double-quoted
       string, the array elements are substituted with a single
       space between each element

     - Ex.

         @names = ("bob", "joe");
         print "@names\n";                # Prints: bob joe
         print @names;                    # Prints: bobjoe.  Print()
                                          #   sees the list argument
                                          #   ("bob", "joe") and 
                                          #   prints the list elements
                                          #   with NO intervening spaces 

         $x = "My name is $names[0]";     # $x is My name is bob
         $names = "pat";
         $x = "My name is $names[0]";     # $x is My name is bob
         $x = "My name is ${names}[0]";   # $x is My name is pat[0]
         $x = "My name is $names\[0]";    # $x is My name is pat[0]
         $x = "My name is $names"."[0]";  # $x is My name is pat[0]




Bob Tarr
University of Maryland, Baltimore County
tarr@umbc.edu