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


In reply to How can I get the four highest valued letters in an array? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.