in reply to In an array get the high value numerals to the lowest and also get its original index values
No coffee yet, so I'm sure more efficient ways will be presented. Anyway, here's one method (note that each on an array is only available on v5.12+):
use warnings; use strict; my @array = (5,2,6,8,10,1,4,7,4,2,21,18,25,10); my %list; while (my ($i,$v) = each @array){ $list{$i} = $v; } print "val: $list{$_}, idx: $_\n" for sort {$list{$a} <=> $list{$b}} keys %list; __END__ val: 1, idx: 5 val: 2, idx: 1 val: 2, idx: 9 val: 4, idx: 8 val: 4, idx: 6 val: 5, idx: 0 val: 6, idx: 2 val: 7, idx: 7 val: 8, idx: 3 val: 10, idx: 4 val: 10, idx: 13 val: 18, idx: 11 val: 21, idx: 10 val: 25, idx: 12
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: In an array get the high value numerals to the lowest and also get its original index values
by Anonymous Monk on Apr 20, 2016 at 07:38 UTC |