HTML CSS PHP PERL

Putting Things into Lists and Arrays

 
Previous Table of Contents Next

Putting Things into Lists and Arrays

Putting things into a literal list is easy. As you just saw, the syntax for a literal list is a set of parentheses enclosing scalar values. The following is an example:


(5, 'apple', $x, 3.14159)


This example creates a four-element list containing the numbers 5, the string 'apple', whatever happens to be in the scalar variable $x, and pi.

If the list contains only simple strings, and putting single quotation marks around each string gets to be too much for you, Perl provides a shortcutthe qw operator. An example of qw follows:


qw( apples oranges 45.6 $x )


This example creates a four-element list. Each element of the list is separated from the others by whitespace (spaces, tabs, or newlines). If you have list elements that have embedded whitespace, you cannot use the qw operator. This code works just as though you had written the following:


('apples', 'oranges', '45.6', '$x')


Notice that the $x is encased in single quotation marks. The qw operator does not do variable interpolation on elements that look like variables; they are treated as though you wanted them that way literally. So '$x' is not converted to whatever the value of the scalar variable $x is; it's left alone as a string containing a dollar sign and the letter x.

Perl also has a useful operator that works in literal lists; it's called the range operator. The range operator is designated by a pair of periods (..). The following is an example of this operator:


(1..10)


The range operator takes the left operand (the 1) and the right operand (the 10) and constructs a list consisting of all the numbers between 1 and 10, inclusive. If you need several ranges in a list, you can simply use multiple operators:


(1..10, 20..30);


The preceding example creates a list of 21 elements: 1 through 10 and 20 through 30. Giving the range operator a right operand less than the left, such as (10..1), produces an empty list.

The range operator works on strings as well as numbers. The range (a..z) generates a list of all 26 lowercase letters. The range (aa..zz) generates a much larger list of 676 letter pairs starting with aa, ab, ac, ad and ending with zx, zy, zz.

Arrays

Literal lists are usually used to initialize some other structure: an array or a hash. To create an array in Perl, you can simply put something into it. With Perl, unlike other languages, you don't have to tell it ahead of time that you're creating an array or how large the array is going to be. To create a new array and populate it with a list of items, you could do the following:


@boys=qw( Greg Peter Bobby );


This example, called an array assignment, uses the array assignment operatorthe equals sign, just as in a scalar assignment. After that code runs, the array @boys contains three elements: Greg, Peter, and Bobby. Notice also that the code uses the qw operator; using this operator saves you from having to type six quotation marks and two commas.

Array assignments can also involve other arrays or even empty lists, as shown in the following examples:


@copy=@original;

@clean=();


Here, all the elements of @original are copied into a new array called @copy. If @copy already had elements before the assignment, they are now lost. After the second statement is executed, @clean is empty. Assigning an empty list (or an empty array) to an array variable removes all the elements from the array.

If a literal list contains other lists, arrays, or hashes, these lists are all flattened into one large list. Observe this snippet of code:


@boys=qw( Greg Peter Bobby );

@girls=qw( Marcia Jan Cindy );

@kids=(@girls, @boys);

@family=(@kids, ('Mike', 'Carol'), 'Alice');


The list (@girls, @boys) is flattened by Perl to a simple list containing first all the girls' names and then all the boys' names before the values are assigned to @kids. On the next line, the array @kids is flattened, and the list ('Mike', 'Carol') is flattened into one long list; then that list is assigned to @family. The original structures of @boys, @girls, @kids, and the list ('Mike', 'Carol') are not preserved in @familyonly the individual elementsfrom Greg tHRough Alice. In other words, the preceding snippet for building @family is equivalent to this assignment:


@family=qw(Marcia Jan Cindy Greg Peter Bobby Mike Carol Alice 

);


The left side of an array assignment can be a list if it contains only variable names. The array assignment initializes the variables on that list. Consider this example:


($a, $b, $c)=qw(apples oranges bananas);


Here, $a is initialized to 'apples', $b to 'oranges', and $c to 'bananas'.

If the list on the left contains an array, that array receives all the remaining values from the right side, no matter where it is in the list. The reason is that an array can contain an indefinite number of elements. Observe the following:

In this example, $a is set to 'peaches'. The remaining fruits in the list on the right are assigned to @fruit on the left. No elements are left for $c to receive a value (because the array on the left side of an assignment absorbs all the remaining values from the right), so $c is set to undef.

It's also important to note that if the left side contains more variables than it has elements, the leftover variables receive the value undef. If the right side has more variables than the list on the left has elements, the extra elements on the right are simply ignored. Here is another example to help understand that concept:

In the first line, $t, $u, and $v all receive a value from the right side. The extra right-side element ('quail') is simply not used for this expression. In the second line, $a, $b, and $c all receive a value from the right. $d, however, has nothing to get from the right ($c takes the last value, 'gopher'), so $d is set to undef.

    Previous Table of Contents Next
    © 2000- NIV