Perl array order to highest numbers

This example Perl-Script find the highest number in the array with the highest number and set it to 1 and then count up to the lowest.

use strict;

print join($",makeRank(9,3,6,19)).$/;
print join($",makeRank(3,3,3,3,3,5,1)).$/;

sub makeRank {
my %hash;
my @rank = sort {$a<=>$b} grep {!$hash{$_}++} @_;
my @result = @_;
for my $i (0..$#rank){
    for my $x (0..$#result)
    {$result[$x] = $#rank-$i if($rank[$i]==$result[$x]);}
}
return map{++$_} @result;
}

Result looks like this:

2 4 3 1
2 2 2 2 2 1 3

One thought on “Perl array order to highest numbers”

  1. Quality posts is the main to invite the users to pay a quick visit the site,
    that’s what this web site is providing.

Leave a Reply

Your email address will not be published.

one × 1 =