w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Perl sort by hash value in array of hashes or hash of hashes

Don't call keys on a reference. Call it on the actual hash.

Also, this $test{$a}->, should be $test->{$a}, because $test is a hash reference.

foreach (sort { $test->{$a}{'value1'} <=>
$test->{$b}{'value1'} } keys %{$test} ){
    print "key: $_ value: $test->{$_}->{'value1'}
"
}

If you had use strict; and use warnings; turned on, you would've gotten the following error to alert you to an issue:

Global symbol "%test" requires explicit package name




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.