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