[Courses] Courses] [Perl] Part 14: Arrays

Dan Richter daniel.richter at wimba.com
Fri Oct 31 10:08:18 EST 2003


LinuxChix Perl Course Part 14: Arrays

1) Introduction
2) Declaring an Array
3) Accessing the Elements of an Array
4) Assigning to an Array
5) I/O with Arrays
6) Exercise
7) Answer to Previous Exercise
8) Past Information
9) Credits
10) Licensing

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

1) Introduction

First of all, I want to apologise for not sending this to the list last 
week. I had to attend a funeral.   :-(

But anyway, let's talk about Perl...

Up until now we have only looked at scalar variables. It's high time we 
had a look at arrays.

Arrays in Perl are very powerful. This week we're only going to look at 
the basics, but don't get in the habit of "micro-managing" your arrays. 
Perl includes powerful macro-commands to do most of what you would 
logically want to do. We will see some of these commands in the 
following weeks.

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

2) Declaring an Array

Array variables are declared just like scalar variables, except that 
they are preceded by an at-sign rather than a dollar sign:

   my @foo;

Note that "@foo" and "$foo" are totally different variables. You can use 
them both in the same code block; they won't interfere with each other 
in any way.

That wasn't so hard, was it?

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

3) Accessing the Elements of an Array

Array elements are accessed using the standard "[]" operator - but with 
a twist. When a single element is accessed, the array name must be 
preceded by a dollar sign rather than an at-sign:

   my @list = ('some', 'words');
   print $list[0] . "\n";

As "man perldata" puts it: "$foo[1] is a part of @foo, not a part of 
$foo. This may seem a bit weird, but that's okay, because it is weird."

Don't let the humility of those words fool you: the system is actually 
quite clever when you fully understand it. The "[]" indicates that the 
variable is an array; the dollar sign indicates that what you want from 
it is a scalar. It may seem obvious that you would want a scalar, but 
Perl includes the ability to "slice" an array by specifying multiple 
subscripts:

   my @chars = ('A', 'B', 'C', 'D', 'E', 'F');
   my @a = @chars[1,5];     # Gets B and F.
   my @b = @chars[1 .. 4];  # Gets B, C, D and E.
   my @c = @chars[1,3..5];  # Gets B, D, E and F.

When you slice, you get back an array, so the variable name is preceded 
by an at-sign.

Note that array subscripts always start at zero. (You can change this by 
setting the special Perl variable "$[" - but that's a REALLY bad idea.)

A negative subscript is interpreted as though it had been added to the 
length of the array, so negative numbers can thought of as "counting 
backwards from the end":

   my @chars = ('a', 'b', 'c', 'd', 'e', 'f');
   print "$chars[-2]\n";    # Prints "e".

It's not an error to access a non-existant subscript; you just get back 
"undef" (the undefined value).

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

4) Assigning to an Array

Perl provides many ways to assign to an array. One is a way we have 
already seen:

   my @x = ('foo', 'bar', 'baz');

Sometimes it's easier to use the "qw//" (quote words) operator:

   my @x = qw/foo bar baz/;   # Equivalent to the above.

Note that the words in "qw//" should NOT be separated by commas.

We can also assign one array to another:

   my @foo = qw{ The "qw//" operator can use any delimiter. };
   my @bar = @foo;

Note that the assignment is done by value, not by reference; subsequent 
changes to "@foo" will not affect "@bar".

Another way to assign to an array is using subscripts:

   $x[6] = 'whatever';
   @y[1,4,9] = ('one squared', 'two squared', 'three squared');

It's not an error to assign beyond the end of the array: the extra 
indices will be automatically created and assigned the value "undef":

   my @arr = qw/just three elements/;
   $arr[50] = 'xyz';      # No problem.

Note that arrays in Perl can contain any scalar:

   my @foo = ('some text', 3.14, FILE_HANDLE);

But an array CANNOT contain another array:

   $bar[3] = @foo;     # Won't work as expected.
   @bar[3] = @foo;     # Equivalent to: $bar[3] = $foo[0]

However, an array can contain a REFERENCE to an array, because a 
reference is a scalar. We'll see more about this when we learn about 
references.

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

5) I/O with Arrays

As you may recall, the following reads a single line from "FOO":

   $line = <FOO>;

By assigning to an array instead of a scalar, we can read ALL the lines 
from a file handle:

   @lines = <FOO>;    # Read the whole file!

Each element of "@lines" is one line of the file (usually with newline 
characters at the end; we have to "chomp" to remove them).

Using "print" to output an array is exactly the same as calling "print" 
for each element of the array:

   print @lines;

Array subscripts are interpolated inside quotes:

   print "Line 2 is: $lines[2]";      # Works as expected.
   print "Line $i us: $lines[$i]";    # Works as expected.

A whole array can also be interpolated inside quotes. In that case, the 
elements of the array are separated by whatever '$"' is set to (it's a 
space by default):

   print "The list is: @list\n";      # Output whole list.
   print "Parts of the list are: @list[2,$i..$j]\n";   # Works!

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

6) Exercise

The Unix program "tail" outputs the last few lines of a file, or 
standard input if no file is given. Write your own "tail" program in Perl.

Because we're just starting out with arrays here, we'll make three 
simplifying assumptions:
1) We assume that the input will not have fewer lines than the number of 
lines you want to display.
2) We do not allow the user to specify the number of lines to display on 
the command line (as real "tail" does).
3) We indiscriminantly read the entire file into memory regardless of 
its size.

You will notice that we haven't discussed yet how to get the length of 
an array. That's okay: we don't need the length here. Use a slice with 
negative subscripts.

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

7) Answer to Previous Exercise

Here is my "polite cat" program:

   #!/usr/bin/perl -w
   use strict;

   while ( <> ) {
     s/\bzero\b/not too much/g;
     s/\bunemployed\b/at a carreer crossroads/g;
     print;
   }

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

8) Past Information

Part 1: Getting Started
         http://linuxchix.org/pipermail/courses/2003-March/001147.html

Part 2: Scalar Data
         http://linuxchix.org/pipermail/courses/2003-March/001153.html

Part 3: User Input
         http://linuxchix.org/pipermail/courses/2003-April/001170.html

Part 4: Control Structures
         http://linuxchix.org/pipermail/courses/2003-April/001184.html

Part 4.5, a review with a little new information at the end:
         http://linuxchix.org/pipermail/courses/2003-July/001297.html

Part 5: The "tr///" Operator
         http://linuxchix.org/pipermail/courses/2003-July/001302.html

Part 6: The "m//" Operator
         http://linuxchix.org/pipermail/courses/2003-August/001305.html

Part 7: More About "m//"
         http://linuxchix.org/pipermail/courses/2003-August/001322.html

Part 8: The "s///" Operator
         http://linuxchix.org/pipermail/courses/2003-August/001330.html

Part 9: Simple File Access
         http://linuxchix.org/pipermail/courses/2003-September/001340.html

Part 10: Executing Commands with "open"
      http://linuxchix.org/pipermail/courses/2003-September/001344.html

Part 11: Perl Variables
      http://linuxchix.org/pipermail/courses/2003-October/001345.html

Part 12: Side Effects with Perl Variables
      http://linuxchix.org/pipermail/courses/2003-October/001347.html

Part 13: Perl Style
      http://linuxchix.org/pipermail/courses/2003-October/001349.html

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

9) Credits

Works cited: man perldata

Thanks to Jacinta Richardson for fact checking.

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

10) Licensing

This course (i.e., all parts of it) is copyright 2003 by Alice Wood and 
Dan Richter, 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