Hi,
I was wondering if anyone could help me with figuring out how to sort by value in descending order. Right now, I'm getting my inputs from different reports generated from different sources. I have the following foreach loop:
foreach $Name(sort keys %x) {
$total_x= $x{$Name};
$total_y= $y($Name};
if ($total_y> 0) {
$total_value= $total_x/$total_y;
printf INTFILE "%-50s %10.3f %15.2f %10.3f %10.5f\n", $Name, $
+total_x, $total_y, $total_value;
}
The output file looks like:
Name x y total_value
abc 3 4 5
xyz 2 1 8
I want to sort the output such that everything is sorted in descending order based on the total_value, so the output should be:
Name x y total_value
xyz 2 1 8
abc 3 4 5
The problem is that the computation of total_value is done inside the loop, so I was wondering if there was anyway around it. I need to keep the foreach loop to get the values of x and y. Thanks.
-r
Edit by dws to add <code> tags
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.