in reply to Numerical sorting issues with a hash of hashes

It's doing what you told it to do. sort sorts as strings, by default. Replace your inner sort with sort { $a <=> $b }, and all shall be well.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: &bull;Re: Numerical sorting issues with a hash of hashes
by Anonymous Monk on Jul 22, 2003 at 14:39 UTC
    hmm...I cleaned it up with the follwoing code:
    foreach my $key ( keys %event_hash ) { print $key, "\n"; for my $key2 ( keys %{ $event_hash{$key} } ) { sort { $a <=> $b } print "$key2=$event_hash{$key}{$key2}\n"; } print "\n"; }
    and the output still isn't sorted:

    OUTPUT

    cycVolumeSetActivityState 1=started 2=startedAutofix 3=completed 10=deleted 4=completedWithMiscompares 11=priorityChanged 5=abortedDueToIOError 20=running 12=scheduled 6=abortedDueToIOErrorWithMiscompares 21=aborted 13=modified 7=abortedByOperator 14=failedToStart 8=abortedByOperatorWithMiscompares 9=failed 15=abortedNoMemory 16=restarted 17=suspended 18=resumed 19=stopped
      You need to call sort in your for condition It only needs a small change to sort out
      foreach my $key ( keys %event_hash ) { print $key, "\n"; for my $key2 ( sort { $a <=> $b } keys %{ $event_hash{$key} } ) { + print "$key2=$event_hash{$key}{$key2}\n"; } print "\n"; }