next up previous contents
Next: Functions for Array and Up: Arrays and Lists Previous: Using a List in   Contents


Creating Arrays

As mentioned in Section 2.4, you can assign the elements of a array through a parenthesized, comma-separated list of values. This is useful for small lists of constants which are set inside a program, but in most practical situations, the list will need to be created through programming statements, either reading data from a file or standard input, or through calculations which take place inside a program. The function push is very useful in creating lists; you provide it with an array, and a value, list, or array of values, and it appends those values to the end of the list. Since perl doesn't require you to declare or initialize variables, you can either jump right in and call push, or you can first create an empty list (by assigning the value () to the list), and then start using push.

Suppose we need to create an array each of whose elements is the contents of a line in a file. To keep the example simple, we'll use the angle bracket while loop described in Section 1.4; recall that this construct sets the special variable $_ equal to the contents of each line of input in turn:

     @lines = ();
     while(<>){
         chomp;
         push(@lines,$_);    
     }
The first line, assigning an empty list to the variable @lines is optional, but you may find it useful to include it in your programs as a reminder that you are ``creating'' an array in the loop which follows. The chomp function will remove a newline from the input if it is present, and is often used in input loops like this one. Note that there is generally no need to count the number of elements read, or to explicitly use indices to assign the values to the appropriate elements.

Another function which is very useful in creating lists is the split function. This function takes a scalar character value, and returns a list containing the ``pieces'' of the value, broken up wherever some specified character or characters occur. By default, one or more occurences of white space (blanks, tabs, newlines, form feeds, etc.) is used to split the string, but an alternative can be specified as the first argument to split. The second argument to the function is the string to be split, which defaults to $_ if not specified. So in the most common case of splitting the default line based on the presence of white space, just the function name alone will provide the desired result. The qw quoting operator (See Section 3.2) provides a similar capability.

The counterpart to split is join, which takes an array and combines the pieces to create a scalar string. join is discussed in Section 4.4.

When you call a function that returns a list, you may only be interested in a part of that list, and don't wish to save the entire list in an array variable. You can use subscripts on a call to a function which returns a list, but you must surround the function call with parentheses before applying the subscript. Suppose we have a character variable named $string, and we wish to extract just the first word of the string. The split command will return a list of all the words in the string; by surrounding the call to split with parentheses, we can use subscripts to extract just the first element:

 $first = split(' ',$string)[0]    # generates an error!!
 $first = (split(' ',$string))[0]  # extracts first word
This same technique can be used with array slices (Section 4.5) to extract larger portions of a list from a function which returns one.

Keep in mind that when you combine arrays and/or lists, they ``roll out'' to make longer and longer arrays. In perl, it's not possible to have an element of an array which is itself an array, although a similar capability can be achieved by using references (see Section [*]). If you try to store an array as a (scalar) element of another array, the rules for arrays in scalar context discussed in Section 4.2 hold, so that the number of elements in the array will be stored, not the array itself. The following lines of code illustrate some of these issues.

     @one = ('dog','chicken','cat','bird');
     @two = (1,5,7,9,11);
     @three = (@one,@two);  # @three has 9 elements
     @hold = ()
     $hold[0] = @one;
     $hold[1] = @two;  # @hold now contains (4,5)

To access some or all of the elements of an array, perl provides a variety of control statements for loops discussed in Section 5.7 and Section 5.6. In addition, the map and grep functions (Section [*]) provide further flexibility in processing arrays.


next up previous contents
Next: Functions for Array and Up: Arrays and Lists Previous: Using a List in   Contents
Phil Spector 2002-10-18