[Courses] [Perl] Part 10: Array Functions

Dan dan at cellectivity.com
Fri Jun 3 19:02:30 EST 2005


LinuxChix Perl Course Part 10: Array Functions

1) Introduction
2) push, pop, shift, unshift
3) split and join
4) sort and reverse
5) Consider the Context
6) Exercises
7) Answers to Previous Exercises
8) Acknowledgements
9) Licensing


          ----------------------------------------

1) Introduction

Last week we saw some basic uses of arrays. This week we're going to
look at the powerful functions that Perl provides for manipulating
arrays.

Next week we're going to see how to index arrays. The reason we save
that for last is that you don't need to index arrays very often: Perl
does most of the low-level array manipulation for you.

          ----------------------------------------

2) push, pop, shift, unshift

"push" and "pop" add and remove items from the end of an array.

  my @arr = ('A', 'B', 'C', 'D');
  my $popped = pop(@arr);      # now $popped = 'D'
  push(@arr, 'X', 'Y');        # now @arr = A B C X Y

"shift" and "unshift" are similar, but they add or remove items from
the beginning. If you don't pass an array to "shift", the list of
command-line parameters ("@ARGV") is used instead. This is the most
common use if "shift".

          ----------------------------------------

3) split and join

"split" splits a string:

  my $password_file_entry = 'root:x:0:0:root:/root:/bin/bash';
  my @fields = split(/:/, $password_file_entry);
  print "@fields\n";    # Output is: root x 0 0 root /root /bin/bash

The first parameter to "split" is a regular expression, so it can
split strings in very sophisticated ways.

The opposite of "split" is "join":

  my @path = ('/bin', '/usr/sbin', '/usr/bin' );
  print join(':', @path);      # Output is: /bin:/usr/sbin:/usr/bin

          ----------------------------------------

4) sort and reverse

"sort" returns an array in alphabetical order (NOT numerical order).

  my @animals = ('gopher', 'bat', 'yak');
  my @sorted = sort(@animals);
  print "@sorted\n";      # Output is: bat gopher yak

"sort" allows you to give a code block or a reference to a comparison
function as the first argument. We haven't gotten to function
references yet, but the following convenient trick allows you to sort
in numerical order:

  @arr = sort( {$a<=>$b} @array_of_numbers );

"reverse" reverses the order of an array. This is NOT the same as
sorting it in reverse order, unless the array was already sorted.

          ----------------------------------------

5) Consider the Context

Many Perl functions and operators behave differently depending on the
"context". Perl has three possible contexts: scalar context, list
context and void context:

  my @arr = ('foo', 'bar', 'baz');
  my @a = reverse(@arr);       # Array context
  my $s = reverse(@arr);       # Scalar context
  reverse(@arr);               # Void context
  
  print "@a\n";       # Output is: baz bar foo
  print "$s\n";       # Output is: zabraboof (foobarbaz backwards)

Note that the context is determined by how the return value is used,
NOT the parameters passed to the function/operator. Many languages
allow a function to change according to its input, but Perl is the
only language that I know that allows a function to change its
behaviour depending on how its output is used!

The use of an array in scalar context returns the length of the
array.

In the above cases the context was obvious because we assigned to an
array or scalar variable. But the context can also be indicated by an
operator. For example, arithmetic operators are all scalar, as is
string concatenation:

  print "The length of the array is: " . @array . "\n";

          ----------------------------------------

6) Exercises

a) Write a program that mimics the Unix command "sort". (No, you
don't have to implement all the command-line options; just read from
standard in and write to standard out.)

b) Write a summation program that accepts input composed of positive
integers separated by plus signs with any amount of space in between.
For example, use it to compute this line:

  15   +  5+100+  22+   1

Remember, There Is More Than One Way To Do It (TIMTOWTDI).

          ----------------------------------------

7) Answers to Previous Exercises

a) The following program strips HTML tags from a file:

  #/usr/bin/perl -w
  use strict;
  
  open MY_INPUT,  "< file1.html" or die "Couldn't open input file:
$!";
  my @lines = <MY_INPUT>;        # Read whole file.
  close MY_INPUT;
  
  foreach my $line (@lines) {
    $line =~ s/<.*?>//g;
  }
  
  open MY_OUTPUT, "> file2.html" or die "Couldn't open output file:
$!";
  print MY_OUTPUT @lines;
  close MY_OUTPUT;

b) If no scalar variable is specified in a "foreach" loop, the
special variable "$_" is used.

c) The "grep" command in Perl compares returns all elements of an
array that meet a certain condition. Each element is stored
sequentially in "$_" an the condition is tested. Examples of "grep"
statements:

  @a = grep /foo/, @b;        # Containing "foo"
  @a = grep( $_ > 5, @b );    # Numbers greater than 5
  @a = grep defined, @b;      # Filter undef.

As with all Perl functions, parentheses are optional with "grep".

          ----------------------------------------

8) Acknowledgements

A big thank you to Jacinta Richardson for suggestions and
proofreading. More advanced Perl users might want to check out the
free material from Perl Training Australia
<http://www.perltraining.com.au/>, which she is a part of.

Other contributors include Meryll Larkin.

          ----------------------------------------

9) Licensing

This course (i.e., all parts of it) is copyright 2003-2005 by Dan
Richter and Alice Wood, and is released under the same license as
Perl itself (Artistic License or GPL, your choice). This is the
license of choice to make it easy for other people to integrate your
Perl code/documentation into their own projects. It is not generally
used in projects unrelated to Perl.



More information about the Courses mailing list