Working with Arrays
Understanding arrays and their basic functions
We will now work a little more with arrays and see some basic functions. Those functions are not the only the ones available for arrays but are the most commonly used.
Pop
Push
Shift
Unshift
Sort
Reverse
Pop:
The pop function simply takes the last element of a array out. That element can be put in a variable.
$pop_value = pop @array1;Or can be discarded
Pop @array1;
Push:
It is the opposite of pop. It writes a new element of your choice at the end of the array.Push array1 , new_element;
Shift:
Shift removes the First element of an array. As with pop the element can be saved in a variable
$shift_value = shift @array1;Or discard
Shift @array1;
Unshift:
It is the opposite of shift. It writes a new element of your choice at the beginning of the array.
Unshift (@array1 , “new_element1”);
What push and unshift have in common is that they can add more than one new element at a time.
Push (@array1 , “new_element1”, “new_element3”, “new_element3”);
unshift (@array1 , “new_element1”, “new_element3”, “new_element3”);
or you can add another array to the array.
Push (@array1, @array2);
Sort:
Sort sorts the element of an array. Its implementation however depends on what type of date you want to sort.
To sort an array of strings :
@array1 = sort @array1;When you re-declare the same variable name you simply copy the content of the array in the same array. The line above will copy the sorted array back into itself. Leaving you with @array1 sorted. You can of course print “sort @array1”. That line will only print the sorted array and not modify it.
You can specify the way sort will sort the value with the cmp function.
@array1 = sort {$a cmp $b} @array1;
that line will sort alphabetically as sort does by default. @array1 = sort {$b cmp $a} @array1;
that line will sort in revert alphabetically. @array1 = sort {uc($a) cmp uc($b)};
@array1 = sort {$a <=> $b} @array1;
This sort will sort the integer from low to high.the “uc” function will sort it by case on top of sorting it alphabetically.
{$a <=> $b }
This sort will sort integer from high to low.
{$a <=> $b }
This sort will sort integer from low to high.
Reverse
Reverse is used to reverse the element of an array. Its implementation is simple
reverse(@name-of-your-array);
it would return your array beginning with the last element down to the first.
Split
First we will split a scalar variable into 2 values in an array.
#!/usr/bin/perl
#author : you
#date : 5.10.2007
#purpose : File I/O split and join
$split1 = “data1 data2”;
@array1 = split (/+s/, $split1);
while (@array1) { print “$array1”;}
#END
The split function needs two elements to process the data. First is what delimiter to use. The delimiter we use in the script above “+s” will split everything separated by space and it is also the default one that is used is no specific delimiter is specified. You can of course use anything “,” “.” “/” etc…. The delimiter needs to be between “ ”.The second element is the data to process, in our case the scalar variable $split1.The script will create a array @array1 and input data1 in array1[0] and data2 in $array1[1].
Join
Join is the opposite of split. It will take elements of an array and join them into a scalar variable. The syntax is almost identical, only the delimiter declaration is different going from “/ /” to “ “ “ “. Being the opposite of split the delimiter we specify this time is used to separate the elements in the newly created scalar.
#!/usr/bin/perl #author : you #date : 5.10.2007 #purpose : File I/O split and join @join1 = (“data1” ,“data2”); $scalar1 = join ( “t“, @join1); print “$scalar1”; #ENDThis script will join the 2 values inside the array into the scalar variable and put a tab "t" between them