Hello all,
I'm having a bit of trouble sorting hashes numerically.
I create the hash entrys like this:
my %hash = ();
$hash{$recNo} {$i} {'first'} = $currS;
$hash{$recNo} {$i} {'last'} = $lastS;
$hash{$recNo} {$i} {'min'} = $minA;
$hash{$recNo} {$i} {'max'} = $minB;
For each $recNo there can be a lot of $i - both $recNo and $i are numeric.
Everything is stored properly and I can easily access it, but I can't manage to sort $i numerically.
Here is how I (try to) do it:
my $rHash = \%hash;
for my $k1 = (sort keys %$rHash) #outer key, this works
{
for my $k2 (sort ( {keys %{rHash->{$k1}{$a}} <=> keys %{$rHash->{$k1
+}{$b}} } keys %{$rHash->{$k1}}) )
{
#display miscellaneous
print $k1 $k2;
}
}
$k1 is in the correct order, $k2 is not (rather random it seems). If I am doing an ascii sort for $k2, like this:
for my $k2 (sort keys %{$rHash->{$k1}})
it sorts correctly by ascii value, which doesnt help me much, but atleast indicates that it should be possible to sort it :)
Also, $k1 seem to be sorted correctly (numerically), why is this? I thought it sorted on ascii values unless asked to do otherwise with the <=> operator.
If someone could shed some light on this (particularly the sorting of $i) I would be very gratefull.
rgds,
Ole C.
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.