photo
Jordan Sissel
geek

Sun, 20 Jun 2004

Removing duplicates from arrays in Perl

After needing to do this in a project of mine, I went googling and found, as expected, a wide variety of solutions. Solutions ranged from using map, foreach, grep, etc.. All using things like a temporary hash to count instances and ensure uniqueness - but I remembered that hashes have unique keys and that hashes are often treated the same way as arrays in perl, so my solution is as follows:

my %foo;  #Temp var
my @a = qw (hello there hello how are you today there what now hello hello hello);

%foo = @a;
@a = keys(%foo);

I also have a one-liner version:

# Assumedly, @a is already defined and has stuff in it, perhaps...

@a = do { my %foo = @a; keys(%foo) };

Comments: 3 (view comments)
Tags: ,
Permalink: /geekery/118
posted at: 19:19


3 responses to 'Removing duplicates from arrays in Perl'

Laura posted at Wed Oct 3 18:38:57 2007...
This doesnt work

Laura posted at Wed Oct 3 19:25:18 2007...
This works:

my %temp;

for (my $i = 0; $i < scalar(@a); $i++){
  $temp{$a[$i]} = $a[$i];
}

@a = keys(%temp);

Jordan Sissel posted at Wed Oct 3 19:55:00 2007...
Hmm, I must not've been thinking when I wrote that.

@a = qw (hello there hello how are you today there what now foo hello hello hello);
@a = do { %tmp = map(($_,0), @a); keys(%tmp)};
print join("\n", @a)';

This works.


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment:


Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '08 Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials SSH Tunneling (Video)

Tag Cloud

Calendar

< June 2004 >
SuMoTuWeThFrSa
   1 2 3 4 5
6 7 8 9101112
13141516171819
20212223242526
27282930   

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati