Manipulating Arrays

Now that you've learned the basic rules for building arrays, it's time to learn some tools to help you manipulate those arrays to perform useful tasks.

Stepping Through an Array

In Hour 3, “Controlling the Program's Flow,” you learned about making loops with while, for, and other constructs. Many tasks you'll want to perform involve examining each element of an array. This process is called iterating over the array. One way you could do so is to use a for loop, as follows:

@flavors=qw( chocolate vanilla strawberry mint sherbet );
for($index=0; $index<@flavors; $index++) {
    print "My favorite flavor is $flavors[$index] and..."
}
print "many others.\n";

The first line initializes the array with ice cream flavors, using the ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.