On Sun, Nov 29, 2015 at 03:30:53PM +0530, Pritish Pattanaik wrote:
> *Remove duplicate elelments from an array, It will maintain the original
> order*
>
> __CODE__
> @array = qw(11 2 3 4 55 4 3 2);
> %hash = ();
> for(my $i=0;$i<=$#array;$i++){
> # store the position from array. use array element as key n position as
> value
> $hash{$array[$i]} = $i if ( ! exists $hash{$array[$i]} ) ;
> }
> @array = ();
> push(@array, $_) for (sort { $hash{$a} <=> $hash{$b}} keys %hash ) ;
> print "Array:@array\n";
> __END__
Ooh, you're working far too hard!
my @array = qw(11 2 3 4 55 4 3 2);
my %seen;
my @unique = grep !$seen{$_}++, @array;
This method is mentioned in the Perl Cookbook that was linked to earlier
in the thread. But I doubt that link should have been online, so get
hold of a legal copy if this is useful to you.
--
Paul Johnson - [email protected]
http://www.pjcj.net
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/