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

  1. Path: sparky!uunet!mcsun!uknet!cam-cl!doc.ic.ac.uk!doc.ic.ac.uk!not-for-mail
  2. From: dds@doc.ic.ac.uk (Diomidis D Spinellis)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Removing duplicates from an array
  5. Message-ID: <18sod5INNgk7@swan.doc.ic.ac.uk>
  6. Date: 12 Sep 92 12:39:01 GMT
  7. References: <lb27g1INNaef@jethro.Corp.Sun.COM>
  8. Organization: Department of Computing, Imperial College, University of London, UK.
  9. Lines: 19
  10. NNTP-Posting-Host: swan.doc.ic.ac.uk
  11.  
  12. In article <lb27g1INNaef@jethro.Corp.Sun.COM> tmhoff@oogoody.Corp.Sun.COM writes:
  13. >Is there a cool way of removing duplicates from an array? I can think
  14. >of several methods, it could be an option on sort, but what is the
  15. >perl way?
  16. If you you know that no array element contains a \0 character, then the
  17. following will do the job:
  18.  
  19.     %a = split("\0", join("\0\0", @a));
  20.     @a = keys %a;
  21.  
  22. The first line generates a list of key value pairs out of the array.
  23. All the values of the pairs are empty.  The list is then assigned to
  24. the associative array.  The second line extracts the unique keys from
  25. the associative array into the normal array.
  26.  
  27. Diomidis
  28. -- 
  29. Diomidis Spinellis    Internet: <dds@doc.ic.ac.uk>  UUCP: ...!uknet!icdoc!dds
  30. Department of Computing, Imperial College, London SW7     #include "/dev/tty"
  31.