eWebProgrammer eweb  

Perl Basics   «Prev  Next»
Lesson 16Arrays and hashes together
Objective Learn how to create multiple associations from one table of data.

Perl Array Hash Association

Sometimes you want several associations from one table of data.
For example, you can create the %viceprez hash from two separate arrays:

@presidents = ( 
  'Franklin D. Roosevelt', 'Harry S. Truman',
  'Dwight D. Eisenhower', 'John F. Kennedy', 
  'Lyndon B. Johnson', 'Richard M. Nixon', 
  'Gerald R. Ford', 'Jimmy Carter',
  'Ronald W. Reagan', 'George Bush', 
  'Bill Clinton' ); 

@viceprez = ( 
  'Henry A. Wallace', 'Alben W. Barkley',
  'Richard M. Nixon', 'Lyndon B. Johnson', 
  'Hubert H. Humphrey', 'Spiro T. Agnew', 
  'Nelson A. Rockefeller', 'Walter F. Mondale',
  'George H. Bush', 'J. Dan Quayle',
  'Al Gore, Jr.' );

for ($i = 0; $presidents[$i]; $i++) {
 $viceprez{$presidents[$i]} = $viceprez[$i];

The values of one array can be used to key into several hashes.
Adding this definition to our example,

@tookoffice = ( 
1933, 1945, 1953, 1961, 1963, 1969, 
1974, 1977, 1981, 1989, 1993 );
for ($i = 0; $presidents[$i]; $i++) {
  $tookoffice{$presidents[$i]} = $tookoffice[$i]; 

This could be printed:
foreach $p (@presidents) {
  print "$p: ",
  "VP: $viceprez{$p}, ",
  "took office: $tookoffice{$p}\n";

This produces the following result:
Franklin D. Roosevelt: VP: Henry A. Wallace, took office: 1933 
Harry S. Truman: VP: Alben W. Barkley, took office: 1945 
Dwight D. Eisenhower: VP: Richard M. Nixon, took office: 1953 
John F. Kennedy: VP: Lyndon B. Johnson, took office: 1961 
Lyndon B. Johnson: VP: Hubert H. Humphrey, took office: 1963 
Richard M. Nixon: VP: Spiro T. Agnew, took office: 1969 
Gerald R. Ford: VP: Nelson A. Rockefeller, took office: 1974 
Jimmy Carter: VP: Walter F. Mondale, took office: 1977 
Ronald W. Reagan: VP: George H. Bush, took office: 1981 
George Bush: VP: J. Dan Quayle, took office: 1989
Bill Clinton: VP: Al Gore, Jr., took office: 1993

One nice alternative to this method of keying multiple data from one list requires more complex data structures, which we will cover later in this module.

Note: a common mistake in working with hashes is to use the keys function to step through the hash like this:
foreach $p (keys %viceprez) {
  print "$p: $viceprez{$p}\n" 

That works fine, as long as your hash is small. The problem is that the keys function copies the entire hash in memory, which can cause swapping or worse if your hash is large. It is usually good to get into the habit of using each instead for the same result:

while(($p, $v) = each %viceprez) { 
  print "$p: $v\n"; 

You get the same data, in the same order, without copying the whole hash in memory.

Array Hash - Exercise

We have covered quite a bit of ground in the past few lessons, and you should now have a pretty good grasp of arrays and hashes.
Take the time now to complete the exercise in which an HTML table using Perl will be created.
Click the exercise link to complete the Array Hash - Exercise
You will build on this knowledge when you move on to the more-complex data structures in the next lesson.