supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Perlmonks,
I am interested to place the four highest valued letters in an array in descending order from two arrays x and y. Array x represents the letters and array y corresponds to the respective numerical value of each element of x. For example, 'a' refers to 9 and 'b' to 6 and so on. I have written a script which finds out the letters in descending order i.e. a b c d for the corresponding values 9 6 5 4. But I have failed to place the letters in an array. I look forward to the suggestions of perlmonks to sort out this problem.
Here goes my script:
#!/usr/bin/perl use warnings; use strict; my @x=qw/d b c e a f/; my @y=qw/4 6 5 2 9 1/; my @sorted_idx=sort{$y[$a]<=>$y[$b]}0..$#y; print "\n #################################### 4 highest valued letters: ####################################\n"; descending_order (reverse @sorted_idx[$#sorted_idx-3..$#sorted_idx]); sub descending_order {my @indices=@_; print " $x[$_] " for @indices; } exit;
I have got the results in cmd as follows:
C:\Users\S>cd d*
C:\Users\S\Desktop>y3.pl
####################################
4 highest valued letters:
####################################
a b c d
C:\Users\S\Desktop>Expected Results:
I want the results in an array so that I can
further operate on the array elements:
my @array=qw/a b c d/; # in descending order of values
print "\n @array\n";
Result: a b c d
|
|---|