Hey all;
I need to find the top $x number of scores out of a hash table, where the scores can be large or small (1 to millions). The hash table is in the format of $Stats{$PortNumber} = $score.
Initially I thought that I could simply pull the top $x (10) scores by obtaining their keys ($PortNumber) for use later in the program. So, I used the following code:
$NumOfPortsToGet = 10;
$c = 0;
foreach $value (sort {$Stats{$a} cmp $Stats{$b}} keys %Stats) {
if ($c < $NumOfPortsToGet && $value ne "UnauthOrigin") {
$Ports[$c] = $value;
$c++;
}
}
However, after trying this, it would always come back with entries that had a score of 99. After printing out the entire list of scores, it became immediately apparent what the problem was. I have other scores like 98347, but those are ranked down with the 98 scores. This happens throughout the rankings.
I thought about zero-padding the numbers, but I do not know how large the numbers may actual get in the wild (they could get extremely large); making it difficult to efficiently zero-pad the numbers (if that would even work).
Does anyone have any idea how I could get the REAL value sort out of this? I haven't been able to find anything as yet through searches.
Thanks for everyones help and take care.
UPDATE: FIXED - STUPID MISTAKE (cmp change to <=>) thanks JediWizard
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.