home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5893 < prev    next >
Encoding:
Internet Message Format  |  1992-09-12  |  1.1 KB

  1. Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Removing duplicates from an array
  5. Message-ID: <1992Sep13.045030.16150@netlabs.com>
  6. Date: 13 Sep 92 04:50:30 GMT
  7. References: <lb27g1INNaef@jethro.Corp.Sun.COM> <18sod5INNgk7@swan.doc.ic.ac.uk>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 21
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <18sod5INNgk7@swan.doc.ic.ac.uk> dds@doc.ic.ac.uk (Diomidis D Spinellis) writes:
  14. : In article <lb27g1INNaef@jethro.Corp.Sun.COM> tmhoff@oogoody.Corp.Sun.COM writes:
  15. : >Is there a cool way of removing duplicates from an array? I can think
  16. : >of several methods, it could be an option on sort, but what is the
  17. : >perl way?
  18. : If you you know that no array element contains a \0 character, then the
  19. : following will do the job:
  20. :     %a = split("\0", join("\0\0", @a));
  21. :     @a = keys %a;
  22.  
  23. Or, if you don't care to worry about what your keys contain, use a slice:
  24.  
  25.     %a = ();
  26.     @a{@a} = (1) x @a;
  27.     @a = keys %a;
  28.  
  29. Of course, neither of those preserve order.  If you want to preserve order,
  30. use the grep solution.
  31.  
  32. Larry
  33.