This is a quick reference of perl Array operations.

1. Init an Array

@listitems=();
@nested=("meat","vegetable","fruit","juice","cock");
@nested=("meat","vegetable","fruit",("juice","cock"));

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

More:

@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
END_OF_HERE_DOC

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

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

2. Printing Array

@array=("red","blue","yellow");
print "I have ", @array, " marbles.\n";
print "I have @array marbles\n.";

Output:

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;

Or

$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"

Output:

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
}

Or

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

6.Appending One array(or item) to another

Use push

push(@array1,@array2);
push(@array1,$item);

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

@front=splice(@array,0,$N);

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]
   push(@reversed,$array[$i]);
}

11. Searching string in an array

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

For exact element search

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

For loose check

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

For non case sensitive

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


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

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