Search this site


Metadata

Articles

Projects

Presentations

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) };

3 responses to 'Removing duplicates from arrays in Perl'

Showing last 3 comments... (Click here to view all comments)

Laura wrote at Wed Oct 3 15:38:57 2007...
This doesnt work

Laura wrote at Wed Oct 3 16: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 wrote at Wed Oct 3 16: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: