Connecting Tech Pros Worldwide Help | Site Map

Perl Functions

By Blair Ireland
Senior Editor, TheScripts.com

There are a lot of handy functions that perl allows. Now I will discuss with you some of the functions for handling arrays. So lets get started;

@one_to_five = ("one","two","three","four","five");
$value = pop(@one_to_five);
# returns "five", the last value of the array one_to_five.
$value = shift(@one_to_five);
# returns "one", the first value of the array one_to_five.

These examples all put the value into the scalar variable I decided to call $value. This is not necessary though, you could just use pop(@one_to_five) to remove the last value of the array, or shift(@one_to_five) to remove the first value of the array. They both remove an element of the array. Data can also be added to an array with ease; You can also add data to an array:

push(@one_to_five,"six");

# adds "six" to the end of the @one_to_five array

@six_to_nine = ("six","seven","eight","nine");
push(@one_to_five,@six_to_nine);
# this will append @six_to_nine to the end of @one_to_five

The following is a short list of handy functions used in the manipulation of arrays;

sort(@one_to_five)
# this sorts the array @one_to_five alphabetically
reverse(@one_to_five)
# this will totally invert or reverse the array @one_to_five
$#one_to_five
# this will give you the length of the array @one_to_five
join(", ",@one_to_five)
# if you would like to join all the values together of
# @one_to_five in one single string, you would use this command.
# All the values will be separated by a "," in this string.

« Perl Variables Perl Operators »