home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7442 < prev    next >
Encoding:
Text File  |  1992-12-15  |  1019 b   |  40 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
  3. From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: Removing duplicate array members. How?
  5. Message-ID: <mcook.724441321@fendahl.dev.cdx.mot.com>
  6. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  7. Nntp-Posting-Host: fendahl.dev.cdx.mot.com
  8. Organization: Motorola Codex, Canton, Massachusetts
  9. References: <1girg7INNh3b@ub.d.umn.edu>
  10. Distribution: usa
  11. Date: Tue, 15 Dec 1992 17:42:01 GMT
  12. Lines: 26
  13.  
  14. serickso@ub.d.umn.edu (Scott Erickson) writes:
  15.  
  16. >  I am trying to remove duplicate members from an array.
  17.  
  18. To me, that always spells "associative array".  If your array already exists,
  19. you can uniquify it by doing this:
  20.  
  21.   undef %b;
  22.   for (@a) { %b{$_} = 1; }
  23.   @a = keys %b;
  24.  
  25. Or this:
  26.  
  27.   undef %already;
  28.   @a = grep(!$already{$_}++, @a);
  29.  
  30. Better would be to keep the list uniqued as you are building it.  Instead of
  31. doing something like
  32.  
  33.   push(@a,$name)
  34.  
  35. do this:
  36.  
  37.   $a{$name}=1
  38.  
  39. Michael.
  40.