Perl ArraysLearning about arrays is a rather big subject, but they are extremely useful. So take your time on this page and learn well.
Arrays are groups of data stored in a single spot. While scalar data (single pieces of data) use the $ sign, arrays use the @ symbol.
For an example, you could make an array using the days of the week :
Notice we used a list for entering the data into an array area.
Arrays are normally entered between the "shebang" line and the MIME line; Similar to specifying things in the HEAD section of HTML. This is not a solid rule though.
Using the double quotes, the script parsed the information and the contents of the array were printed out with a space between each data entry.
A shortcut for eliminating the quotes in a list area is qw. The above example would have the array line changed to :
This method only works when you are dealing with one word selections though. qw symbolizes a quoted comma deliminated list.
Arrays are able to contiain regular data (as seen above), other arrays, scalar data, hashes, and/or expressions. Arrays are pretty flexible when it comes to data.
Using the week days example, to print Thursday the code would be :
Hold it! Is that an error? Why is the symbol a $ instead of @?
An array is a group of data. Scalar items are single data elements. We are wanting to display a single data item, thus a scalar symbol is used.
What about that  part? Isn't Thursday the 4th day?
The coding will find the 4th data value in the named array. Remember that the array index starts at zero so spot #3 holds the 4th data item.
Here is the days array broken down into its parts and values visually :
You could actually assign the values into the array like that too, but doesn't the LIST way look a lot smaller and easier?
Now try adding this to the example coding...
Before you run your script, what do you think the result will be? Highlight the area below to find the answer!
Negative numbers can be used to find information starting at the end of an array list. The last value is -1, the second to last value is -2, and so forth (or back as the case may be).
Besides regular and negative numbers, scalar data and expressions may also be used as the index value.
Now lets work with arrays a bit. The most part about working with arrays is what you want to do with the information. You've seen how to set array data using a LIST above. Here are some other commonly used situations :
If you want to assign the first value of an array into a scalar, the script would be :
Since a scalar variable can only hold one piece of data, it will take the first element of the array automatically. This does not change the array values at all.
To assign the first two elements of an array into scalar values :
Using the round brackets for the above examples are important. Without them, the script will pass the length of the array (the number of elements the array is holding) into the scalar variable.
If that was the weekdays array, the result scalar variable would have a value of 7.
A similar idea is to find the last index number in an array. Add in a $ and it will provide :
But that really isn't the true length of the array considering the array index starts at zero. So to find the number of elements, the amount will have to be adjusted by one.
To copy one array to a new (second) array :
To add an new value to the beginning of an array, the UNSHIFT command is used :
To add a new value to the end of an array also has two options :
Next is combining two arrays into a new array :
After all of that adding, it's time for a bit of subtraction. To remove the first value of an array the SHIFT command is used :
You can store that removed value into a scalar at the same time too :
To remove the last element of an array :
To remove the last element of an array and store it in a scalar :
To replace a specific element in an array :
Now we can go into a different aspect of working with arrays. The following will display various sorting or arranging techniques. The contents of the array are not changed or harmed unless you save the result into a new array or back into the same array.
To sort an array in ASCII (Alphabetical) order :
Example of storing a sort result back into the same array :
To sort an array in reverse ASCII (Alphabetical) order :
To sort an array in numeric ascending order :
To sort an array in numeric decending order :
To print a sorted array but not alter the original :
To reverse the value elements in an array :
Array slices : two or more elements of an array being accessed at the same time.
We have seen how to find a single entry from an array previously on this page. Now it's time to find muliple entries.
The first part to notice is the stored result is going into an array. When we were dealing with only one value, using a scalar variable was OK, but we are moving into multiple values, so an array is the choice.
The second part shows comma deliminated numbers. The script will go into the array at those specific array index points, retrieve the values, and store them into the new array.
Let's bring in our weekdays example :
After the above command gets executed, the contents of the @values array would be :
Notice that the new array used its normal numbering instead of 0 3 4. That's because the 0 3 4 was a reference into the array being looked at, not into the new array. If you wanted to get the same index numbers, then the code would have to be changed to this :
As like all other examples, the numbers specified for the index reference can be scalar variables, expressions, etc..
The value assignment can work backwards too (or forwards depending on your view on things). If you want to assign specific values to specific array spots :
This page was created in 0.00112 seconds
Comments and Questions
Last modified: January 08 2008.