Chapter 4. Arrays
Introduction
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.
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 ("Reagan",
"Bush",
"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 @presidents
=
("Reagan",
"Bush",
"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 push and
pop) require a proper array and not merely a list.
Think of the difference between $a and
4. You can say $a++ but not
4++. Likewise, you can say
pop(@a) but not pop
(1,2,3).
The most important thing to glean from this is that Perl’s ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access