This is a quick reference of perl Array operations.

1. Init an Array


Note: The second and third are equivalent, because perl doesn't support netsted lists directly.


@shopinglist=qw( meat vegetable fruit juice cock);

Or for a lot of multi word elements, use a here document and extract lies

@lines=(<<"END_OF_HERE_DOC" =~ m/^\s*(.+)/gm);
You can initialize a large array in this way,
However, it would fill your program with values and make it hard to read

Use this way instead if you want put a file into an array

open(DATA, "<datafile") or die "Couldn't read file\n";
while( <DATA> ) {
    push(@bigarray, $_);

2. Printing Array

print "I have ", @array, " marbles.\n";
print "I have @array marbles\n.";


I have redblueyellow marbles.
I have red blue yellow marbles.

3. Changing Array size

You can enlarge or truncate an array using the way below:

$#array = $new_last_element_index_number;


$array[$new_last_element_index_number]= $value;

Note: $#array is the number of the last valid index in @array, @array(in scalar context, as when treated as a number) is the number of elements. $#array is one less than @array because array indices start at 0.

See this example, suppose array @students has 21 student names:

print " The class now has ", scalar(@students), " students.\n";
print "The index of the last element is $#students.\n"


The class now has 21 students
The index of the last element is 20

4. Doing something with every element in a list

Use foreach loop:

foreach $item (@list) {
   #do something like print $item;

Or for loop, same as foreach $item (@array)

for $item (@array) {
#do something

another way, same as foreach $_ (@array)

for (@array) {
#do something

5. Iterating over an array by reference

Use foreach or for to loop over the dereferenced array:

#iterate over elements of array in $arrayref
foreach $item (@$arrayref) {
  #do something


for ($i=0;$i <=$#$arrayref; $i++) {
    #do something with $arrayref->[$i]

6.Appending One array(or item) to another

Use push


7. Remove an element from array(front or end)

Use pop or shift

$item=pop(@array) removes an item from the end of the array
$item=shift(@array) removes an item from the front of the array

8. Processing multiple elements of an array

Use splice

remove $N elements from front of @array


similariy, remove $N elements from end of the array

@END = splice(@array,-$N);

9. Sorting an array numerically

Want to sort a list of numbers ? here is it

@sorted = sort {$a <=> $b} @unsorted;

10. Reverse an array

Use reverse function

#reverse @array into @reversed

@reversed = reverse @array;

Same to the for loop

for ( $i = $#array; $i>=0; $i--) {
   # do something with $array[$i]

11. Searching string in an array

@food = ("pears","eels","apples","aaa");

For exact element search

grep /^$a$/, @food )
/^$a$/, @food )

For loose check

grep /$a/, @food )
/$a/, @food )

For non case sensitive

grep /$a/i, @food )
/$a/i, @food )

Note: all examples above return all matches, use this to catpture the matches

@matches=grep /^$a$/, @food )




Comments powered by CComment