Over the course of this tutorial, I'll be examining Perl's arrays in detail, explaining what they are, how they work, and how you can use them to get things done faster, better and cheaper. In addition to providing a gentle introduction to Perl arrays and hashes in general, this article will also offer you a broad overview of Perl's array manipulation functions, providing you with a handy reference that should help you write more efficient code.
# add an element to the beginning of the array unshift (@meals, "breakfast");
# array now looks like this @meals = ("breakfast", "lunch", "tea");
When dealing with associative arrays, however, it's not a good idea to use these functions, since key-value pairs in a Perl associative array are not always stored in the order in which they were defined. Therefore, to remove an element from an associative array, you should instead use the delete() function, as in the example below:
The grep() function can tell you whether or not a particular value exists in an array. It accepts two arguments, a pattern to match and the array in which to search, and it scans every element of the named array for elements matching the pattern. Results, if any, are returned as another array. The following example illustrates how this works: