PDA

View Full Version : Sorting number arrays


rockdoc
04-01-2006, 07:30 AM
Hello,

I'm trying to find out how many values in @sorted are less than or equal to the values in @unique

@sorted = (3 12 16 56 56 78 92 99 99)

@unique = grep { ++$count{$_} < 2 } @sorted; # (3 12 16 56 78 92 99)


i.e.

There is 1 value in @sorted less than or equal to 3

... 2 values in @sorted less than or equal to 12

... 3 values in @sorted less than or equal to 16

... 5 values in @sorted less than or equal to 56

etc etc

I just want to return those values to another array

@count = (1 2 3 5 etc );

Any suggestions?

KevinADC
04-01-2006, 02:04 PM
The index number of the last array position of each unique number is what you want.

my @sorted = (3, 12, 16, 56, 56, 78, 92, 99, 99);
my %results = map {$sorted[$_],$_+1} (0..$#sorted);
my @results = sort {$a <=> $b} values %results;
print "@results";

rockdoc
04-02-2006, 03:47 PM
Thankyou KevinADC.

Some very clever coding there. It works beautifully. :thumbsup: