home *** CD-ROM | disk | FTP | other *** search
/ ftp.cse.unsw.edu.au / 2014.06.ftp.cse.unsw.edu.au.tar / ftp.cse.unsw.edu.au / pub / doc / languages / perl / nutshell / ch2 / against next >
Encoding:
Text File  |  1992-10-18  |  674 b   |  29 lines

  1. #!/usr/bin/perl
  2.  
  3.     # First open my database.  Complain if unable.
  4.  
  5. open(STUFF, "stuff") || die "Can't open stuff: $!\n";
  6.  
  7.     # Load inverted array.
  8.  
  9. while (<STUFF>) {
  10.  
  11.     # Split the record into its fields.
  12.  
  13.     ($beastie, $noses, $hazard, $premium, $servants)
  14.     = split(/:/, $_);
  15.  
  16.     # Append to current list.  (Note .= assignment operator.)
  17.  
  18.     $beastie_list{$hazard} .= $beastie . ",";
  19. }
  20.  
  21.     # Now print the inverted entries out.
  22.  
  23. foreach $hazard (sort keys(%beastie_list)) {
  24.     chop($beastie_list{$hazard});         # Delete final space.
  25.     print $hazard;
  26.     print " " x (16 - length($hazard));   # "tab" to column 16.
  27.     print $beastie_list{$hazard}, "\n";
  28. }
  29.