Arrays, Array Operators
This document was written by CS 290W TA David Corcoran and was last
modified
Perl supports Arrays much differently than other programming
languages. In Perl, since all data is scalar data, arrays can consist
of data of any type mixed and matched. Array names begin with the
"@". After that they follow the same rules as scalar names.
Literal Representation of an Array
# This will contain the values 1 through 5.
@aiArray = (1,2,3,4,5);
# This is the same thing as above.
@aiArray = (1..5);
# You can set one array equal to another.
@aiNewArray = @aiArray;
# This will contain letters A-D.
@acArray = ('A','B','C','D');
@acArray = ('A'..'D');
# You can even mix and match.
@amArray = ('A',1, "Jim Bob", 2);
You access values of the array just like in other programming languages.
So, in Perl all array indices also start at 0.
@aiArray = (1,2,3,4,5);
print "The fourth value is $aiArray[3] \n";
# Gets the Size of the Array
$iSizeOfArray = @aiArray;
print "Size of the array is $iSizeOfArray \n";
# prints "Size of the array is 5"
The above shows how to access elements of the array. You can even put
arithmetic operations and variables within the array brackets. Notice
how you can determine the size of the array. Perl keeps track of it
for you. All you have to do is to use the array name without any
subscript to get the size of the array. Perl also has a mechanism
($#...) to get the subscript of the current last element of the array.
@aiArray = (1,2,3,4,5);
print "Subscripts range from 0 to $#aiArray \n";
# prints "Subscripts range from 0 to 4"
You can even shorten (or lengthen) an array by changing $#....
@aiArray = (1,2,3,4,5);
$#aiArray = 2;
print "@aiArray \n";
# prints "1 2 3"
Reverse Operator
@aiArray = (1,2,3,4,5);
@aiRevArray = reverse( @aiArray );
# Same thing
@aiRevArray = reverse( 1,2,3,4,5 );
The above statements will reverse the order of the array. Instead of
having 1,2,3,4,5, @aiRevArray will contain 5,4,3,2,1.
Sort Operator
@asArray = ("Dave","Allen","Bob");
@asSortArray = sort( @asArray );
The above statement will sort the contents of the array in ascending
ASCII order. @asSortArray will now contain "Allen","Bob","Dave".
Foreach Statement
@aiArray = (1,2,3,4,5);
foreach $iValue ( @aiArray )
{
print "$iValue";
}
The above statement basically steps through every element of the array and
assigns it to $iValue. You may then perform operations on the current
element within the block structure. The above structure will print out
1, 2, 3, 4, 5.
Associative Arrays
An Associative Array is an array in which items are paired. The first
is called the "key" and the second is called the "value". An
associative array is identified by a leading "%".
%asDay = ("mon", "Monday", "tue", "Tuesday",
"wed", "Wednesday", "thu", "Thursday",
"fri", "Friday", "sat", "Saturday",
"sun", "Sunday");
To locate a particular "value", its "key" is used as a subscript
within curly braces.
# This prints the string "Friday"
print "$asDay{'fri'} \n";
# This also prints the string "Friday"
print "$asDay{fri} \n";
# This prints the string "Wednesday"
$sWhichDay="wed";
print "$asDay{$sWhichDay} \n";
# This adds another day to the associative array
$asDay{"frog"}="Froggyday";