Perl Basics   «Prev  Next»

Perl Data Structure - Exercise

Print a Presidential Table



#!/usr/bin/perl

@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.', );

@tookoffice = ( 1933, 1945, 1953, 1961, 1963, 1969, 1974,
  1977, 1981, 1989, 1993 );

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

for ($i = 0; $presidents[$i]; $i++) {
  $tookoffice{$presidents[$i]} = $tookoffice[$i]; }
  
print "content-type: text/html\r\n\r\n";

print "<table border=1>\n";
print "  <tr> <th>President<th>Vice President<th>took office\n";
foreach $p (@presidents) {
 print "  <tr> "; 
 print "<th align=left>$p\n",
  "    <td>$viceprez{$p}\n",    
  "    <td align=center>$tookoffice{$p}\n";
}     
print "</table>\n";

Paste your code here: