How-to: Perl Arrays

In this tutorial we will cover Perl Arrays. Perl Arrays are a good way to keep a list of elements handy in memory for further reference later in your program. If you want to have a list, Perl Arrays are a good way to do this. Perl Arrays are more intensive on resources than hashes but this have valid uses.

In many cases you will see a program populate a Perl Array with data, then look through each element in the Perl Array, and then perform some function.

Basic Perl Array

[adrotate banner=”3″]
To start we will keep things simple. We are simply going to create a Perl Array with a few elements and then print all the elements in the array.

#!/usr/bin/perl

use Data::Dumper;
use strict;
use warnings;


my @array =('blue', 'green', 'red', 'yelow');

print Dumper(@array);

This will give you an output like:

./testarray.pl

$VAR1 = 'blue';
$VAR2 = 'green';
$VAR3 = 'red';
$VAR4 = 'yelow';

Populating Perl Arrays

In most real world situations, you will not have static array elements like the above example. For the next example, we will read in a file split each row into individual elements and then populate those new elements into rows in our array. Lastly, we print the whole Perl Array using Data::Dumper.
[adrotate banner=”3″]
The list of users is in the same format as from our Perl Hash Tutorial. 

ID=1 First=John Last=Doe AGE=42
ID=2 First=Jane Last=Doe AGE=35
ID=3 First=Jack Last=Doe AGE=17
ID=4 First=Jill Last=Doe AGE=15

Use the following code example to access the list.

#!/usr/bin/perl

use Data::Dumper;
use strict;
use warnings;

getPeople();

sub getPeople {
        my $file = 'list.txt';
        my @people = ();
        open( LIST, "< $file" ) or die "Can't open $file : $!";
        while (my $row = <LIST> ) {
                my ($id, $first, $last, $age) = (split /[\s=]/, $row)[1, 3, 5, 7];
                push(@people, "$id, $first, $last, $age");

        }
print Dumper(@people);

close LIST;
}

When run you will get this for your output:

./testarray.pl

$VAR1 = '1, John, Doe, 42';
$VAR2 = '2, Jane, Doe, 35';
$VAR3 = '3, Jack, Doe, 17';
$VAR4 = '4, Jill, Doe, 15';

Accessing Elements in a Perl Array

Dumping all elemenets in an array using Data::Dumper is a great tool, but what if you want to access just certain elements of an array? In this example we will show you how to reference individual Perl Array Elements, and go one step further by splitting those rows into smaller elements and printing just what we need.

!/usr/bin/perl

use Data::Dumper;
use strict;
use warnings;

getPeople();

sub getPeople {
        my $file = 'list.txt';
        my @people = ();
        open( LIST, "< $file" ) or die "Can't open $file : $!";
        while (my $row = <LIST> ) {
                my ($id, $first, $last, $age) = (split /[\s=]/, $row)[1, 3, 5, 7];
                #### Populate the Array.
                push(@people, "$id, $first, $last, $age");

        }
print Dumper(@people);

# Get just the first and last name from the Array Element
my ($first, $last) = (split(/,\s/, $people[3]))[1 ,2];
print "The person with ID 3 is $first $last\n";

close LIST;
}
This will give you the following output:
./testarray.pl

$VAR1 = '1, John, Doe, 42';
$VAR2 = '2, Jane, Doe, 35';
$VAR3 = '3, Jack, Doe, 17';
$VAR4 = '4, Jill, Doe, 15';
The person with ID 3 is Jill Doe

 

Conclusion

Perl Arrays are a great way to store and access lists of information in your programs. Hopefully you have found the above guide useful. Check back in a few weeks when we will expand on our pervious Perl Array and Perl Hash tutorials, by using arrays of arrays and arrays of hashes.

-Tutor


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *