A Perl array variable stores an ordered list of scalar values.
To refer a single element of Perl array, variable name will be preceded with dollar ($) sign followed by index of element in the square bracket.
@arrayName = (element1, element2, element3..);
This is a simple example to use Perl array.
#!/usr/bin/perl @num = (2015, 2016, 2017); @string = ("One", "Two", "Three"); print "$num[0]\n"; print "$num[1]\n"; print "$num[2]\n"; print "$string[0]\n"; print "$string[1]\n"; print "$string[2]\n";
In the above example, we have defined two arrays, one with number element and other with string element. Both arrays are printed with their index elements.
To access a single element of a Perl array, use ($) sign before variable name. You can assume that $ sign represents singular value and @ sign represents plural values.
Variable name will be followed by square brackets with index number inside it. Indexing will start with 0 from left side and with -1 from right side.
@months = qw/Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec/; print "$months[0]\n"; print "$months[1]\n"; print "$months[7]\n"; print "$months[9]\n"; print "$months[6]\n"; print "$months[-3]\n";
The size of an array is determined with scalar context on the array. The returned value will be always one greater than the largest index. In short the size of an array will be ($#array + 1). Here, $#array is the maximum index of the array.
@array = (you, me, us); $array[5] = 4; $size = @array; $index_max = $#array; print "Size: $size\n"; print "Maximum Index: $index_max\n";
In the output, there are only three elements containing information, but the give array has total 5 elements.
You can add or remove an element from an array using some array functions.
We'll discuss following array Perl functions:
The push array function appends a new element at the end of the array.
@array = ("pink", "red", "blue"); push @array, "orange"; print "@array\n";
In the above program, "orange" element is added at the end of the array.
The pop array function removes the last element from the array.
@array = ("pink", "red", "blue"); pop @array; print "@array\n";
In the above program, "blue" element is removed from the end of the array.
The shift array function removes the left most element of array and thus shorten the array by 1.
@array = ("pink", "red", "blue"); shift @array; print "@array\n";
In the above program, "pink" is removed from the array.
The unshift array function adds a new element at the start of the array.
@array = ("pink", "red", "blue"); unshift @array, "orange"; print "@array\n";
In the above program, "orange" is added at the start of the array.
The splice array function removes the elements as defined and replaces them with the given list.
@alpha = (A..Z); print "Before - @alpha\n"; splice(@alpha, 8, 8, U..Z); print "After - @alpha\n";
In the above program, the replacement begins counting from 9th position (I) to 8 elements that is P.
With the help of split() function, we can split a string into array of strings and returns it.
# original string $string = "Where-There-Is-A-Will-There-Is-A-Way"; # transforming strings into arrays. @string = split('-', $string); print "$string[4]\n";
In the above program, we have transformed $string into array at hyphen (-) values. Now from this array, we have printed fourth element of the array.
The join() function is used to combine arrays to make a string. It combines the separate arrays into one string and returns it.
# original string $string = "Where-There-Is-A-Will-There-Is-A-Way"; # transforming arrays into strings. @string = split('-', $string); $string_full = join( '-', @string ); print "$string_full\n";
In the above program, the string is splitted at hyphens (-). We have used join() in $string_full and printed it.
Two arrays can be merged together using merged() function as a single string removing all the commas in between them.
#two arrays @array1 = ("Girl", "in", "front", "of", "me"); @array2 = ("is", "very", "beautiful"); #merging both the arrays @merged = (@array1, @array2); print "@merged\n";
In the above program, array1 and array2 are merged into one single string and then printed.
To sort an array, sort() array function is used. The sort() function sorts all the elements of an array according to the ASCII standard.
# defining array @days = ("sun", "mon", "tue", "wed", "thu", "fri", "sat"); print "Original array: @days\n"; # sorting array @days = sort(@days); print "Sorted array: @days\n";
In the above program, we have printed both original and sorted array. This array is sorted in the alphabetical order.