home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / perl / 7930 < prev    next >
Encoding:
Text File  |  1993-01-21  |  1.5 KB  |  64 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!srvr1.engin.umich.edu!uvaarpa!mmdf
  3. From: Alan Stebbens <aks%anywhere@hub.ucsb.edu>
  4. Subject: Re: lists in associative arrays 
  5. Message-ID: <1993Jan21.173318.4445@uvaarpa.Virginia.EDU>
  6. Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
  7. Reply-To: aks%anywhere@hub.ucsb.edu
  8. Organization: The Internet
  9. Date: Thu, 21 Jan 1993 17:33:18 GMT
  10. Lines: 52
  11.  
  12. | From: danq@spot.Colorado.EDU (Daniel Quinlan)
  13. | ...I think there must be some better way to do this
  14. | than ...
  15. |     $list{$i} = "$list{$i} new_entry" ; 
  16.  
  17. | From:    Tom Christiansen <tchrist@convex.COM>
  18. | The ".=" operator would be a bit more efficient, I suspect.
  19.  
  20. The way I build lists is:
  21.  
  22.     $list .= $new.' ';
  23.  
  24. Then, when you're done, I either use 
  25.  
  26.     @items = split(' ',$list);
  27.  
  28. to ignore the trailing blanks when separating items, or to simply remove
  29. the trailing blank:
  30.  
  31.     chop($list);
  32.  
  33. If you are building an array of accumulating lists:
  34.  
  35.     while (<>) {
  36.         ...
  37.         $list[$index] .= $new.' ';
  38.         ...
  39.     }
  40.  
  41. You choose to either ignore the trailing blanks, or chop them:
  42.  
  43.     grep(chop,@list);
  44.  
  45.  
  46. If you are building a associative array of accumulating lists:
  47.  
  48.     while (<>) {
  49.         ...
  50.         $list{$entry} .= $new.' ';
  51.         ...
  52.     }
  53.  
  54. Simiarly, you can ignore the trailing blanks, chop them, or use split
  55. (as Tom pointed out above) to ignore them.
  56.  
  57.     foreach $ent (keys %list) {
  58.         chop($list{$ent});
  59.         }
  60.  
  61. -- Alan Stebbens, UCSB, Santa Barbara <aks@hub.ucsb.edu>
  62. "If all you have is a hammer, then everything looks like a nail."
  63.  
  64.