in reply to Remove redundency from an array
However, since you will be sorting the array, there might be a different approach: first sort it, and since it is sorted, remove duplicate elements, which, because of the sorting, will be sequential.
use strict; use warnings; my @array = (1,2,5,6,4,2,1,2,3,4,6,4,3,2,4,6,6); my @sorted = sort {$a <=> $b} @array; # numerically @array = (sort shift @sorted); # overwrite old array while (my $element = shift @sorted) { # if the element differs from the last added, keep it # otherwise discard if ($element != $array[-1]) { push (@array, $element); } } # array is now sorted, no duplicate elements
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Remove redundency from an array
by Eimi Metamorphoumai (Deacon) on Sep 25, 2007 at 17:00 UTC | |
by lyklev (Pilgrim) on Sep 25, 2007 at 21:28 UTC |