Perl Arrays
In Perl, an array is a special type of variable that contains multiple values. The following string variable, ‘$fname’, has been defined and initialised with the name ‘Bob’.
$fname = "Bob";
If multiple names need to be stored, instead of having a different variable for each, an array could be used. It should be noted that when an array is defined, the name is preceded by an '@' sign, rather than '$'.
my @fname = ();
@fname[0] = "Bob";
@fname[1] = "George";
@fname[2] = "Fred";
@fname[3] = "Alan";
The first line in the above example declares an empty array. Each element in an array has an index number, which is used in lines two to five above to populate the four elements. Notice that the first index number is a zero and not a one.
Another method of populating an array is to use the 'push' function, which achieves the same result as above.
my @fname = (); push(@fname, "Bob"); push(@fname, "Goerge"); push(@fname, "Fred"); push(@fname, "Alan");
The declaration and initialisation of an array can be shortened by combining into one statement.
my @fname = ("Bob", "George", "Fred", "Alan");
As well as being able to use the index number to populate a particular element in an array, it can also be used to extract the value of an element in an array, in order to carry out a particular task, or simply just to display it.
print "$fname[2]";
Here, the element with an index number of two would be displayed, in this case “Fred”. Note that when accessing a single element of an array, its name is preceded by a '$', rather than an '@' sign.
In order to display all of the elements in an array a ‘for’ loop can be used.
my $length = @fname; for (my $i = 0; $i < $length; $i++) { print "$fname[$i]\n"; }
The above example puts the length, or number of values in the array, into a variable called '$length'. This is then used in the condition of the ‘for’ loop. The loop counter variable, '$i' is used to extract each element of the array, which is output to the terminal.
Bob George Fred Alan
This can be simplified by using a ‘foreach’ loop, instead of an ordinary ‘for’ loop.
foreach my $name (@fname) { print "$name\n"; }
If it is necessary to sort the values in an array, then the 'sort' function can be utilised to achieve this.
@fname = sort(@fname);
The 'push' function, discussed earlier, adds another element to the end of an array. There is also a function called 'unshift' that can be used to add another element at the beginning of the array.
unshift(@fname, "Alice");
Similarly, there are functions to remove both the last and first elements of an array. The 'pop' function removes the last element, whilst the 'shift' function deletes the first.
pop(@fname); shift(@fname);
As well as being able to populate arrays with an explicit set of values as with the above examples, it is also possible to use a range.
my @numbers = (1..10); my @letters = ("A".."Z");
The first of these creates an array with ten elements, the numbers one through ten, whilst the second array holds twenty-six elements, comprising of the letter's 'A' through 'Z'.
Lastly, there are functions to split a string into an array and join array elements within a string. These functions are 'split' and 'join' respectively.
my $names = "Bob, George, Fred, Alan"; my @fname = split(", ", $names);
The above example declares a string variable called '$names' that contains four names split by a comma and a space. The second line creates an array called '@fname' that splits the '$names' variable by the comma and space to create four elements in the array, one for each name.
my @fname = ("Bob", "George", "Fred", "Alan"); my $names = join(", ", @fname);
This last example does the opposite of the previous one. It takes an array containing four names and joins them into one string value, separated by a comma and a space.