Siaris
Simple Things
Syndicate: full/short
Siaris
Categories
General0
News2
Programming2
LanguageBits0
Perl50
Ruby10
VersionControl1
Misc1
Article Calendar
<= September, 2010
S M T W T F S
1234
567891011
12131415161718
19202122232425
2627282930
Search this blog

Key links
External Blogs
Brought to you by ...
Ruby
1and1.com

Array Basics

Andrew L. Johnson (First published by ItWorld.com 2000-11-02)

We’ve looked at arrays and some of the basic array operators in a previous article (back in March 2000), so in this article we’ll revisit arrays from a slightly different angle — when and how to use them.

Often I see cases where a list of data is assigned to an equal list of scalar variables as in the following hypothetical database reformatting task:

    #!/usr/bin/perl -w
    use strict;
    while(<>){
        chomp;
        my ($id, $lname, $fname, $department,
            $location, $shift) = split /:/;
        print "$lname|$fname|$id|$shift|$department|$location\n";
    }
    __END__

While assigning each field to a named scalar can be beneficial when many of them will be used in various places in the rest of the block (self documenting code), the above usage is trivial and unnecessary. If we are merely reformatting the data, or perhaps storing it in a larger data structure for later use, we do not need to create all of these variables:

    #!/usr/bin/perl -w
    use strict;
    while(<>){
        chomp;
        my @fields = split /:/;
        print join('|', @fields[1,2,0,5,3,4]),"\n";
    }
    __END__

In this case, we are just reformatting the data so rather than specify each field name in the new ordering, we need only re-join the fields in their new order using an array slice.

Another good place to use an array is in building a hash. That’s right, we can easily use an array as a list of keys when building a hash by using a hash-slice. Consider reading in the same type of data above but this time we wish to build a hash using field names for keys:

    #!/usr/bin/perl -w
    use strict;
    my @fields = qw/id lname fname department location shift/;
    while(<>){
        chomp;
        my %data;
        @data{@fields} = split /:/;
        process(\%data); # do something with %data hash
    }
    # process subroutine definition ...
    __END__

Besides being easy to follow, an advantage of the above method is that if you want to eventually process the data fields in the order they occur, you can loop through the array of keys rather than using the keys() function (which does not return keys in the order they were entered in the hash).

As a final array example, let pick a random element from an array:

    #!/usr/bin/perl -w
    use strict;
    my @array = qw/andrew john greg brad/;
    my $draw  =  @array[rand @array];
    print "I picked $draw\n";
    __END__

This works because the rand() function returns a number from 0 up to but not including the argument passed to it. In the above, it returns a number from 0 to 3.999… — and an array index always uses the integer value of the number provided, so in the above case the index can be 0, 1, 2, or 3. If you want to randomize an entire array in place then the perlfaq’s have a good example of a shuffle routine called the fisher_yates shuffle — see the entry in perlfaq4 entitled:

    How do I shuffle an array randomly?

*****