10 Part 9: Intro to Arrays

LinuxChix Perl Course Part 9: Intro to Arrays

1) Introduction
2) A Simple Example
3) Using foreach
4) Filling an Array
5) Exercises
6) Answers to Previous Exercises
7) Acknowledgements
8) Licensing

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

1) Introduction

As you may recall, there are four basic data types in Perl:
a) scalar - A single chunk of data
b) array - A bunch of scalars, indexed by a number
c) hash (hashtable) - A bunch of scalars, indexed by another scalar
known as a key
d) handle - A pointer enabling the opening of resources from the
operating system (files, directories, etc.)

Up until now we've only looked at scalars and handles, but now we're
going to look at arrays.

Arrays in Perl are both handy and powerful, but it takes time to
learn to use them properly. For this reason, we're going to spend
three weeks looking at arrays:
a) This week we'll see array basics.
b) Next week we'll look at some helpful array functions.
c) The following week we'll see the painful details of arrays.

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

2) A Simple Example

Here's a simple program that outputs what I'll eat for lunch today.

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

my @lunch = ('sandwich', 'apple', 'cookie');
print "My lunch today: @lunch\n";

As you can see, an array variable is preceded by "@" rather than "$".

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

3) Using foreach

The above example wasn't very helpful because we didn't access the
array elements separately. Let's try a slightly more complicated
example.

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

my @lunch = ('sandwich', 'apple', 'cookie');
foreach my $item (@lunch) {
print "I'll eat a $item.\n";
}

In addition to accessing each member of an array, "foreach" can be
used to change members of an array. This program is a demonstration.

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

my @arr = ('metadata', 'meta-information', 'metaphysics');
foreach my $item (@arr) {
$item =~ s/meta-?/useful /;
}
print "@arr\n";

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

4) Filling an Array

There are many ways to fill an array. Here are a few.

a) From a file handle:

my @lines = <HANDLE>; # Read whole file.

b) From another array:

@b = @a;

In the above case, "@b" contains a copy of "@a", not a pointer to it.
Changes to either will not affect the other.

c) Using m//g:

# The following finds everything that comes after the letter "Q".
my $text = "There's an Iraqi squid on my qwerty keyboard!";
my @letters = ( $text =~ m/q([a-z])/g );
print "@letters\n"; # Output is: i u w

The "g" option means a global match: return everything at once.

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

5) Exercises

a) Write a program that reads an HTML file and removes all the HTML
tags, leaving just the text. (Yes, I know we could do this without
arrays, but read the file as an array of lines.)

Note: if spaces magically appear at the beginning of the lines in
your output file, it's because you wrote the "print" statement like
this:
print OUTPUT "@lines"; # Quotes around @lines
Instead, try writing it like this:
print OUTPUT @lines; # No quotes

b) Consider this program:

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

my @arr = ('sing', 'ring', 'fling');
foreach (@arr) {
s/ing/ung/;
}
print "@arr\n";

If no scalar variable is specified in the "foreach" statement, what
variable is used?

c) Consider this program:

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

my @a = ('aardvark', 'beaver', 'ant', 'squirrel');
my @b = grep /^a/, @a; # Match words starting with A.
print "@b\n";

What does "grep" do? "grep" is a very powerful tool, and well worth
reading the manual for:

perldoc -f grep

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

6) Answers to Previous Exercises

a) This is a variation of the program that converts U.S. spelling to
U.K. spelling. This one changes the spelling of e.g. "color" to
"colour".

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

while ( <STDIN> ) {
s/(\w\w[^aeiou])or\b/$1our/g;
print;
}

(Actually, this example isn't as good as the previous one because
there are more exceptions to the rule. For example, the word
"monitor" is spelt the same way in the U.S. and the U.K.)

b) The "ARGV" file handle, which is implied by an empty diamond
operator "<>", treats each command-line parameter as a file name and
reads from each of these files in succession. If no file is specified
on the command-line, it reads from standard input. This is the
standard behaviour for many Unix commands.

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

7) 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.

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

8) 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.