Tweet

Perl arrays

Introduction

Arrays are variables which contain lists of values. Each element in the list is accessed, (or indexed), by a numeric subscript.

We can define an array in Perl as follows:

    my @planets = ('mercury', 'venus', 'earth', 'mars', 'jupiter');

Or more compactly, we can use the qw operator to help us build a list of quoted words. This frees us from having to bother with quoting each element or separating them with commas:

    my @planets = qw(mercury venus earth mars jupiter);

Accessing an individual element of the array is easy, as this complete example shows:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my @planets = qw(mercury venus earth mars jupiter);
    print "The first planet is: " . $planets[0] . "\n";

which outputs:

    The first planet is: mercury

Note that we used the @ character (or sigil), to introduce the array as a whole, (my @planets), and the $ character to access an element within the array, ($planets[0]).

Iterating over an array

This code uses a for loop to iterate over each element in the @planets array:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my @planets = qw(mercury venus earth mars jupiter);

    for my $planet (@planets) {
        print $planet . "\n";
    }

Which outputs:

    mercury
    venus
    earth
    mars
    jupiter

Note that the foreach keyword is a synonym for for.

Adding to an array

We know that our array currently has five elements, the first element, (at index 0), is mercury and the last, (at index 4), is jupiter.

We can assign a new value by simply creating a new index, as in:

    $planets[5] = 'saturn';

But since our list of planets is still obviously incomplete, we can also use the push function to push a list of values onto the end of our array:

    push @planets, qw(uranus neptune pluto);

push() pushes the values of the list in its second argument onto the end of the array supplied as its first argument.

Length of an array

You can determine the number of elements in an array using the scalar function as follows:

    print scalar @planets;

Here is a complete example:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my @planets = qw(mercury venus earth mars jupiter);

    print 'The number of elements in the planets array is: ' . 
        scalar @planets . "\n";

    push @planets, qw(saturn uranus neptune pluto);

    print 'The number of elements in the planets array is now: ' . 
        scalar @planets . "\n";

which outputs:

    The number of elements in the planets array is: 5
    The number of elements in the planets array is now: 9

Alternatively, the $#array construction returns the subscript, or index, of the last element of the array. Since Perl arrays start at element 0, this number will be one less than the length of the array. So the following code sample is another way to display the length of the @planets array:

    print 'The number of elements in the planets array is now: ' .
            ($#planets + 1) . "\n";

The $#array syntax can be used to create a numbered listing of our planets, as in:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my @planets = qw(mercury venus earth mars jupiter saturn uranus 
                     neptune pluto);

    foreach my $orbit (0..$#planets) {
        print $orbit + 1 . " $planets[$orbit]\n";
    }

which outputs:

    1 mercury
    2 venus
    3 earth
    4 mars
    5 jupiter
    6 saturn
    7 uranus
    8 neptune
    9 pluto

Removing the end of array

There is a certain amount of controversy in astronomical circles about whether Pluto is really a planet, or just another Kuiper Belt object.

Let's assume that a decision has been made to remove Pluto from the list of planets. We can remove Pluto in our code as follows:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my @planets = qw(mercury venus earth mars jupiter saturn uranus 
                     neptune pluto);

    print 'The number of planets is: ' .  scalar @planets . "\n";

    # Urgent email from the International Astronomical Union, Pluto no
    # longer a planet...

    pop @planets;

    print 'The number of planets is now: ' .  scalar @planets . "\n";
    print 'and the outermost planet is: ' . $planets[$#planets] . "\n";

This produces the following output:

    The number of planets is: 9
    The number of planets is now: 8
    and the outermost planet is: neptune

The pop function removes the last element from an array, (and returns it).

See also

  perldoc -q array (plenty of good examples)
  perldoc perldata
  perldoc perllol
  perldoc perldsc
  perldoc -f push
  perldoc -f pop
  For more detail, see our arrays tutorial
Revision: 1.3 [Top]