Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to sort the keys of the following hash:
$VAR1 = { 'GetNext -------------- 11.254.49.217:1.3.6.1.2.1.4.20.1.2' => { '1.3.6.1.2.1.4.20.1.3' => [ [ 'ipAdEntNetMask', 1 ] ], 'Min_Time' => '16:29:08.03', 'Error_Status' => 'No error', 'Avg_Time' => undef, 'Min_Time_Diff' => '9:59:59.9', 'Total_Time' => '0:00:00.0', 'Last_Time' => '16:29:08.03', 'Max_Time' => '16:29:08.03', '1.3.6.1.2.1.4.20.1.2' => [ [ 'ipAdEntIfIndex', 1 ] ], 'Max_Time_Diff' => '00:00.0' }, 'Response -------------- 11.254.49.217:1.3.6.1.2.1.4.20.1.3.0.0.0.0' + => { 'Error_Status' => 'No error', 'Total_Time' => '0:00:00.0', 'Last_Time' => '16:29:08.04', '1.3.6.1.2.1.4.20.1.4.0.0.0.0' => [ [ 'ipAdEntBcastAddr.0.0.0.0', 1 ] ], 'Max_Time_Diff' => '00:00.0', 'Min_Time' => '16:29:08.04', 'Avg_Time' => undef, 'Min_Time_Diff' => '9:59:59.9', '1.3.6.1.2.1.4.20.1.3.0.0.0.0' => [ [ 'ipAdEntNetMask.0.0.0.0', 1 ] ], 'Max_Time' => '16:29:08.04' }, 'GetNext -------------- 11.254.49.217:1.3.6.1.2.1.4.20.1.2.127.0.0.2 +' => { 'Error_Status' => 'No error', 'Total_Time' => '0:00:00.0', 'Last_Time' => '16:29:08.04', '1.3.6.1.2.1.4.20.1.3.127.0.0.2' => [ [ 'ipAdEntNetMask.127.0.0.2', 1 ] ], 'Max_Time_Diff' => '00:00.0', 'Min_Time' => '16:29:08.04', '1.3.6.1.2.1.4.20.1.2.127.0.0.2' => [ [ 'ipAdEntIfIndex.127.0.0.2', 1 ] ], 'Avg_Time' => undef, 'Min_Time_Diff' => '9:59:59.9', 'Max_Time' => '16:29:08.04' }, 'Response -------------- 11.254.49.217:1.3.6.1.2.1.2.2.1.12.20' => {
with this code...
my @key_hash = sort { $key_hash{substr($a,3)} cmp $key_hash{substr($b,3)} } keys %key_hash;
I only need to look at the first three letters.
Any idea what I'm doing wrong?

Replies are listed 'Best First'.
Re: Sorting a hash of hashes
by BrowserUk (Patriarch) on Apr 03, 2003 at 15:21 UTC

    If your trying to sort by the first three chars of the key, then you need only to substr the key

    my @key_hash = sort { substr($a,3) cmp substr($b,3) } keys %key_hash;

    If your trying to sort by the first 3 chars of the value, you need to apply the substr to the value

    my @key_hash = sort { substr($key_hash{$a},3) cmp substr($key_hash{$b},3) } keys %key_hash;

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: Sorting a hash of hashes
by jasonk (Parson) on Apr 03, 2003 at 15:11 UTC

    Your hash doesn't have any three letter keys, and your substr doesn't return three letters. Perhaps you meant:

    my @key_hash = sort { substr($a,0,3) cmp substr($b,0,3) } keys %key_hash;

    We're not surrounded, we're in a target-rich environment!
Re: Sorting a hash of hashes
by dragonchild (Archbishop) on Apr 03, 2003 at 15:12 UTC
    We don't know what's going wrong, so we don't know what you're doing wrong. I also have no idea what you're trying to do.

    However, the first thing that comes to mind is that you're attempting to access your hash with a substr of the key. Well, the substring isn't a key to your hash.

    Furthermore, I recognize this data structure as one we've seen many questions on before. Have you even tried to:

    1. Write out what you're trying to do
    2. Figure out what you need to get done
    3. Think at all?!!

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

    A reply falls below the community's threshold of quality. You may see it by logging in.