home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!cam-cl!doc.ic.ac.uk!doc.ic.ac.uk!not-for-mail
- From: dds@doc.ic.ac.uk (Diomidis D Spinellis)
- Newsgroups: comp.lang.perl
- Subject: Re: Removing duplicates from an array
- Message-ID: <18sod5INNgk7@swan.doc.ic.ac.uk>
- Date: 12 Sep 92 12:39:01 GMT
- References: <lb27g1INNaef@jethro.Corp.Sun.COM>
- Organization: Department of Computing, Imperial College, University of London, UK.
- Lines: 19
- NNTP-Posting-Host: swan.doc.ic.ac.uk
-
- 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;
-
- The first line generates a list of key value pairs out of the array.
- All the values of the pairs are empty. The list is then assigned to
- the associative array. The second line extracts the unique keys from
- the associative array into the normal array.
-
- Diomidis
- --
- Diomidis Spinellis Internet: <dds@doc.ic.ac.uk> UUCP: ...!uknet!icdoc!dds
- Department of Computing, Imperial College, London SW7 #include "/dev/tty"
-