Removing duplicates from a Perl array – really this time

My last post got a bit sidetracked and i didn’t do a simple removal of dupes from an array. As I said last time, hashes cannot have duplicate keys, so the easiest way to do this is to simply create a hash, then assign each element in the array to a key with an arbitary value. Stage one consists:

my @test_array = qw/ 1 3 2 4 3 2 4 7 8 2 3 4 2 3 2 3 2 1 1 1 /;
my %temp_hash = map { $_, 0 } @test_array;

I’m using the qw command to add elements into the test array to start with, this requires no spaces in the values, but is quick and dirty in this instance. Once the array is populated, the map function takes the input of the array and outputs a list in form:

1 0 3 0 2 0 4 0 3 0 2 0 4 0 7 0 8 0 2 0 3 0 4 0 2 0 3 0 2 0 3 0 2 0 1 0 1 0 1 0

Which we add straight into a hash. The duplicate keys are ignored so actually we get just one of each key with its assigned ‘0’ value. All thats needed now is to transfer the keys back into an array, to do this we can use the keys function like so:

my @uniq_array = keys %temp_hash;

So putting it all together we get:

#! /usr/bin/perl -w

use strict;
my @test_array = qw/ 1 3 2 4 3 2 4 7 8 2 3 4 2 3 2 3 2 1 1 1 /;
my %temp_hash = map { $_, 0 } @test_array;
my @uniq_array = keys %temp_hash;	
print "@uniq_array\n";

The output is as follows:

8 4 1 3 7 2

All nice and unique! It would be nice to have a function or sub to do this nice and neatly? Here goes:

#! /usr/bin/perl -w

use strict;
	
sub uniq{
	my %temp_hash = map { $_, 0 } @_;
	return keys %temp_hash;
}

my @test_array = qw/ 1 3 2 4 3 2 4 7 8 2 3 4 2 3 2 3 2 1 1 1 /;
my @uniq_array = uniq(@test_array);
print "@uniq_array\n";
This entry was posted in Perl and tagged , , , , . Bookmark the permalink.

Leave a Reply