home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
- From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
- Subject: Re: Removing duplicate array members. How?
- Message-ID: <mcook.724441321@fendahl.dev.cdx.mot.com>
- Sender: news@merlin.dev.cdx.mot.com (USENET News System)
- Nntp-Posting-Host: fendahl.dev.cdx.mot.com
- Organization: Motorola Codex, Canton, Massachusetts
- References: <1girg7INNh3b@ub.d.umn.edu>
- Distribution: usa
- Date: Tue, 15 Dec 1992 17:42:01 GMT
- Lines: 26
-
- serickso@ub.d.umn.edu (Scott Erickson) writes:
-
- > I am trying to remove duplicate members from an array.
-
- To me, that always spells "associative array". If your array already exists,
- you can uniquify it by doing this:
-
- undef %b;
- for (@a) { %b{$_} = 1; }
- @a = keys %b;
-
- Or this:
-
- undef %already;
- @a = grep(!$already{$_}++, @a);
-
- Better would be to keep the list uniqued as you are building it. Instead of
- doing something like
-
- push(@a,$name)
-
- do this:
-
- $a{$name}=1
-
- Michael.
-