Mike Gossland's Perl Tutorial Course for Windows


Introduction | String Functions | Array Functions | Test Functions | User Functions


Chapter 3. Functions

Array Functions

Array functions in Perl are very useful, and as the name suggests they operate on arrays. Some of these functions operate on lists, which are very similar to arrays. I'm going to combine both list and array functions in this section. Arrays can be filled with numbers or strings.

We'll look at the most common array functions:

push
pop
shift

Push

Push takes an array and pushes a new value onto the end of it. If you had an array like

@array = ( "one", "two", "three" );

then

push @array, "four";

would add "four" onto the end after "three".

Pop

Pop does the opposite of push. It returns the top value from the array, and removes it from the array.

$value = pop @array;

would return "four" and put @array back to ( "one", "two", "three" ).

Shift

Shift is like pop, except it returns the first element and then deletes it.

@array = ( "one", "two", "three" );
$value = shift @array;

would put $value equal to "one", and leave @array as ("two", "three");

List Functions

List functions operate on lists. Commonly used functions include:

grep
join
split
map
sort

We will have just a brief look at each function. For more complete explanations, look them up in the online HTML documentation.

Grep

Grep comes from its Unix brother of the same name. Grep is an acronym for "global regular expression print". In scalar context, the function looks through an array or list and returns the number of times the regular expression is found in the elements. The statement

$found = grep /a/,  ( "ant", "bug", "cat" );

would set $found to 2 because there are two occurrences of "a", one in ant and one in cat. Because the value is being assigned to a scalar, Perl evaluates grep in a "scalar context" and the single value 2 is returned.

You can also evaluate grep in a "list context". If you assign the return value of grep to an array instead of to a scalar, then the array will be filled with the matched elements.

@found = grep /a/,  ( "ant", "bug", "cat", );

would leave the array @found with the values like this:

@found = ( "ant", "cat");

Grep is a very powerful tool for determining whether patterns exist in array elements, or extracting those elements out of a list or array that match a certain pattern.

Join

Join is a very useful function. Given a "join string" and a list of values, it will return the string of values separated by the join string. It is very useful for putting a comma and space between list values. No extra ones are put at the beginning or end, just between the elements.

$text = join  ", ", @array;

would set $text to "one, two, three, four" with the right @array.

Split

Split is the converse of join. Split separates a string using a delimiter pattern and returns the separated array. It is very useful for separating text lines that contain fields into their individual parts. 

@array = split /, / , "one,two,three,four";

would result in @array having the values ( "one", "two", "three", "four");

($a, $b, $c, $d) = split /\s*,\s*/, "1 , 2, 3 ,   4";

would result in $a = 1, $b = 2, $c = 3 and $d = 4. Note that /\s*,\s*/ is a regular expression matching any whitespace around a comma.

Map

Map is a bizarre but useful function. It takes a list as an argument and performs a user-defined operation on each element in turn. Then it returns the list consisting of the modified values. For example, to change an array to all upper case, you could write:

@upper = map { uc($_) } @array;

One by one, each element of the array is assigned to the default variable $_  which is then converted to upper case in the expression uc($_).  By the time all the elements are done, map has made a copy of the original array but all in upper case. It returns that new list, which is then assigned to @upper. 

For another example, to print an array with one element per line, you could tack a newline character onto the end of each value like this:

print map { "$_ \n" } @array;

One by one, each element of the array is assigned to the default variable $_  and is quoted with a newline character: "$_\n". By the time all the elements are done, map has a copy of the original array but with a newline added to each element. It returns that new list. The print statement then prints the new list, and each orignal array value comes out on its own line.

Sort

Sort will arrange any list into a sorted order. Many variations are possible so check the documentation. It has much more to offer than I'm showing here.

print sort ("gamma", "beta", "alpha");

would print alphabetagamma. If you wanted commas in between, use join too:

print (join  ", ", sort ("gamma", "beta", "alpha")), "\n";

to get "alpha, beta, gamma". Be sure to look more into sort, because you can get the sort function to sort a list according to virtually any sort criterion you can imagine.

With these functions at your disposal, you are very close to getting into some real coding. Carry on to learn about the interesting test functions.