Tuesday, July 24, 2012

Remove duplicate entries in a perl scalar array

How to remove duplicate entries from perl scalar array.

Ans ==>
my @duplicate_entries = (1,2,3,4,3,22,45,1,22,76,456,12,45,22,876,456,847,14,6,365,7,4,33,5);
my (%hash,@result);
foreach my $x(@duplicate_entries){
    unless($hash{$x}){
        push(@result,$x);
        $hash{$x} = 1;
    }
}

>> @result will be an array of unique elements.

No comments:

Post a Comment