Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

my @array = (5,2,6,8,10,1,4,7,4,2,21,18,25,10);

How can I get the high value numerals to the lowest and also get its original index values?

If two(or more) numbers comes same, both should count.
Ex. 2 & 4

here 25 is the biggest value, we should know it is the biggest and also its index ie, 12
likewise 21 is the 2nd biggest value, we should know it is the 2nd biggest and also should know its index = 10
likewise 18 is the 2nd biggest value, we should know it is the 2nd biggest and also should know its index = 11
.
.
.
4 & its index value
Another 4 & its index value (There are two 4's in the array, likewise there are two 2's in the array)
.
.
.
n number. Here the number with lowest value and what its index is!

  • Comment on In an array get the high value numerals to the lowest and also get its original index values

Replies are listed 'Best First'.
Re: In an array get the high value numerals to the lowest and also get its original index values
by BrowserUk (Patriarch) on Apr 15, 2016 at 11:43 UTC

    Try:

    @array = (5,2,6,8,10,1,4,7,4,2,21,18,25,10);; @orderedIndexes = sort{ $array[ $b ] <=> $array[ $a ] } 0 .. $#array;; print @orderedIndexes;; 12 10 11 4 13 3 7 2 0 6 8 1 9 5

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you BrowserUk
Re: In an array get the high value numerals to the lowest and also get its original index values
by stevieb (Canon) on Apr 15, 2016 at 11:56 UTC

    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
      Thanks stevieb
Re: In an array get the high value numerals to the lowest and also get its original index values -- oneliner
by Discipulus (Canon) on Apr 20, 2016 at 09:05 UTC
    Following stevieb data representation I propose the following, somehow ugly, oneliner
    perl -e "print map{qq(val: $$_[0], idx $$_[1]\n)}sort{$$a[0]<=>$$b[0]} +map{[$ARGV[$_],$_]}0..$#ARGV" 5 2 6 8 10 1 4 7 4 2 21 18 25 10 val: 1, idx 5 val: 2, idx 1 val: 2, idx 9 val: 4, idx 6 val: 4, idx 8 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

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.