Perl Arrays

Learning 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 :
@days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

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.

#!/usr/bin/perl

@days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

print "Content-type: text/html\n\n";

print "These are the days of the week: ";
print "@days";

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 :

@days = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday);

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.



Similar to JavaScript, PERL array items are numbered from 0 instead of 1. These numbers are referred as the array index.

Using the week days example, to print Thursday the code would be :

print "The value for the fourth array item is $days[3]";

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 [3] 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 :
$days[0] = "Monday";
$days[1] = "Tuesday";
$days[2] = "Wednesday";
$days[3] = "Thursday";
$days[4] = "Friday";
$days[5] = "Saturday";
$days[6] = "Sunday";

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...
print "The mystery value is $days[-4]";

Before you run your script, what do you think the result will be? Highlight the area below to find the answer!

THURSDAY

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 :
($result)=@array;

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 :
($result1,$result2)=@array;

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.
$result=@array;

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 :
$result = $@array;

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.
$result = $@array+1;

To copy one array to a new (second) array :
@array1 = @array2

To add an new value to the beginning of an array, the UNSHIFT command is used :
unshift(@array,newelement);
or the same effect can be done without the UNSHIFT :
@array=(newelement,@array);

To add a new value to the end of an array also has two options :
(@array,newelement);
or the slightly longer version :
@array=(@array,newelement);

Next is combining two arrays into a new array :
@newarray=(@firstarray,@secondarray);

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 :
shift(@array);

You can store that removed value into a scalar at the same time too :
$result=shift(@array);

To remove the last element of an array :
pop(@array);

To remove the last element of an array and store it in a scalar :
$result=pop(@array);

To replace a specific element in an array :
$array[number]=$newelement;



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 :
sort(@array);

Example of storing a sort result back into the same array :
@array = sort (@array);

To sort an array in reverse ASCII (Alphabetical) order :
sort {$b cmp $a} (@array);

To sort an array in numeric ascending order :
sort {$a <=> $b} (@array);

To sort an array in numeric decending order :
sort {$b <=> $a} (@array);

To print a sorted array but not alter the original :
print sort(@array);

To reverse the value elements in an array :
reverse (@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.

@values = @array[0,3,4];

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 :
@values = @days[0,3,4];

After the above command gets executed, the contents of the @values array would be :
$values[0]="Monday";
$values[1]="Thursday";
$values[2]="Friday";

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 :
@values[0,3,4] = @days[0,3,4];

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 :
@array[0,3,4] = ("Hello","Out","There");