Specifying a List In Your Program
Printing a List with Commas
Changing Array Size
Doing Something with Every Element in a List
Iterating Over an Array by Reference
Extracting Unique Elements from a List
Finding Elements in One Array but Not Another
Computing Union, Intersection, or Difference of Unique Lists
Appending One Array to Another
Reversing an Array
Processing Multiple Elements of an Array
Finding the First List Element That Passes a Test
Finding All Elements in an Array Matching Certain Criteria
Sorting an Array Numerically
Sorting a List by Computable Field
Implementing a Circular List
Randomizing an Array
Works of art, in my opinion, are the only objects in the material universe to possess internal order, and that is why, though I don't believe that only art matters, I do believe in Art for Art's sake.
- E.M. Forster
If you are asked about the contents of your pockets, or the names of the last three presidents, or how to get to the highway, you recite a list: you name one thing after another in a particular order. Lists are part of your conception of the world. With Perl's powerful list- and array-handling primitives, you can translate this world view directly into code.
In this chapter, we'll use the terms list and array as the Perl language thinks of them. Take
"Clinton"); that's a list of the last three American presidents, in order. To store that list into a variable, use an array, as in
"Clinton"). Both are ordered groups of scalar values; the difference is that an array is a named variable, one whose array length can be directly changed, whereas a list is a more ephemeral notion. You might think of an array as a variable and a list as the values it contains.
This distinction may seem arbitrary, but operations that modify the length of these groupings (like
pop) require a proper array and not merely a list. Think of the difference between
4. You can say
$a++ but not
4++. Likewise, you can say
pop(@a) but not
The most important thing to glean from this is that Perl's lists and arrays are both ordered groupings of scalars. Operators and functions that work on lists or arrays are designed to provide faster or more convenient access to the elements than manual access would provide. Since few actually deal with modifying the array's length, you can usually use arrays and lists interchangeably.
@nested = ("this", "that", "the", "other"); @nested = ("this", "that", ("the", "other"));
Why doesn't Perl (usefully) just support nested lists directly? Although partially for historical reasons, this easily allows for operations (like
sort) that work on arbitrarily long lists of arbitrary contents.
What happens if you want a more complex data structure, such as an array of arrays or an array of hashes? Remember that scalars aren't restricted to containing just numbers or strings; they can also hold references. Complex (multilevel) data structures in Perl are always put together using references. Therefore, what appear to be "two-dimensional arrays" or "arrays of arrays" are always implemented as arrays of array references, in the same way that two-dimensional arrays in C can be arrays of pointers to arrays.
Most recipes in this chapter don't care what you keep in your arrays; for example, the problem of merging two arrays is the same whether the arrays contains strings, numbers, or references. Some problems are intrinsically tied to the contents of your arrays; recipes for those are in Chapter 11, References and Records. This chapter's recipes deal with generic arrays.
@tune = ( "The", "Star-Spangled", "Banner" );
"The" is in the first position, but you'd access it as
"Star-Spangled" is in the second position, but you'd access it as
$tune. This structure is doubly justified: the contrariness of computers, whose first representable number is 0, and the contrariness of language designers, who chose 0 because it is an offset into the array, not the ordinal number of the element.