-->
Previous | Table of Contents | Next |
An array is an indexed list of data elements. Perl provides two different kinds of arrays. The first kind of array is similar to the arrays that are available in most high-level languages. This kind of array stores single data elements (numbers or strings) into a variable that can store any number of elements. You can get the elements you store in this variable by using the offset value of the element you are trying to find. This means that you can assign a number or a string value to an offset in an array and later use that offset to retrieve that value. For example, the following Perl statement assigns the strings January, 1st, 1999 to the array named date
@date = (January, 1st, 1999);
Note:
The number of elements you can have in the array is limited by the amount of memory you have on your system.
If you later want to retrieve one of the values you have placed into the date array, you can do so by stating the array name and the offset of the element you want. When you are retrieving the value of an array element, you must precede the name of the array with a $ instead of the @ that you used to define the array. This is because you are now referring to a single array element, which is really the same as a regular variable. For example, to assign the value of January, which is the first element in the array, to a variable called month, write the following command:
$month=$date[0];
Note:
Like many Linux utilities and both C and C++, Perl starts its array indexes at offset 0. This means that an array of length 5 would have the elements [0], [1], [2], [3], and [4].
The second kind of array that is available in Perl is called an associative array (also called hashes with Perl 5 and later versions. Associative arrays are like normal arraysthey store data elements and have an index. Unlike normal arrays, the index or key is defined by the user and can be a number or a string.
Because you can define the key values for associative arrays as well as the data associated with each of these key values, assigning and accessing data from an associative array is somewhat different than it is with normal arrays. To assign a value to an associative array, you must also provide the key that you want that value to be associated with. Just as with normal arrays, associative arrays have a special symbol that is used whenever you are performing operations on the entire associative array. You usually assign variables only to elements of an associative array as opposed to dealing with the entire array all at once. The following command assigns the value hello to the associative array named words, with a key value of greeting:
$words{greeting} = hello;
An operation that applies to the entire associative array looks like the following:
%words2 = %words;
This command assigns the entire contents of the words associative array to the words2 associative array.
To retrieve a value from the words associative array, use the following command:
$value=$words{greeting};
This assigns the string hello to the value variable.
In addition to the assignment operations that can be performed on normal arrays, associative arrays have four more operations: keys, values, delete, and each.
The keys operator returns a list of all the keys that are defined in the specified associative array. The following commands result in keyarray being equal to (greeting, greeting2).
$words(greeting) = hello; $words(greeting2) = good-bye; @keyarray = keys(%words);
The first two commands assign values to individual elements in the associative array words. The first command assigns hello with a key value of greeting to the array, and the second command assigns good-bye with a key value of greeting2 to the array.
The last command assigns all the key values contained in the words associative array to the normal array called keyarray. Notice the % in front of the words variable in the keys operator. This means that the operation being performed applies to the entire associative array and not just one element of the array.
Note:
The value of keyarray that is returned by the preceding commands could actually be (greeting2, greeting), because values in associative arrays are not stored in any particular order.
Another operator that exists for use with associative arrays is the values operator. The values operator returns a list of all the values contained in the specified associative array. The following commands result in valuearray being equal to (123.5,105) or (105,123.5).
$cost{regular} = 123.5; $cost(sale) = 105; @valuearray = values(%cost);
The first two commands again assign values to individual elements in the cost associative array. The last command executes the values operator on the cost associative array and assigns the results to the normal array called valuearray.
The delete operator enables you to delete elements from an associative array. You call the delete operator on a single associative array element, and it deletes both the value of that element and the key value for that element. The following commands illustrate how the delete operator works.
%coffee = (instant,5.35,ground,6.99); delete $coffee{ground};
The first command assigns two elements to the associative array coffee. One of the elements has a key value of instant and a value of 5.35, and the other element has a key value of ground and a value of 6.99. The delete operator deletes both the key value and the value of the element in the coffee array that has a key value of ground.
Previous | Table of Contents | Next |