K
Kasp
Hello,
Consider an unsorted array
my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
Now, I wish to remove the duplicate elements from this array. A good way may
be to assign these array's elements as keys of a hash. And then convert the
keys back to the array like this:
<UNTEST CODE>
my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
my %hash = ();
for($i=0;$i<=$#array;$i++){
$hash{ $array[$i] } = '';
}
(@array) = (keys %hash); # @array now contains unique elements
</UNTEST CODE>
Is there a better or shorter way?
Thanks.
--
Consider an unsorted array
my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
Now, I wish to remove the duplicate elements from this array. A good way may
be to assign these array's elements as keys of a hash. And then convert the
keys back to the array like this:
<UNTEST CODE>
my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
my %hash = ();
for($i=0;$i<=$#array;$i++){
$hash{ $array[$i] } = '';
}
(@array) = (keys %hash); # @array now contains unique elements
</UNTEST CODE>
Is there a better or shorter way?
Thanks.
--