MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:
my @AoH = ( { Lead => "fred", Wife => "wilma", Son => "bambam", }, { Lead => "george", Wife => "jane", Son => "elroy", }, { Lead => "homer", Wife => "marge", Son => "bart", }); for my $i ( 0 .. $#AoH ) { print "$i is { "; for my $role ( keys %{ $AoH[$i] } ) { print "$role=$AoH[$i]{$role} "; } print "}\n"; }
This gives the following output:
I want to create another array of hashes that sorts the original array of hashes based on the values of the "Son" field, so the output becomes:0 is { Son=bambam Lead=fred Wife=wilma} 1 is { Son=elroy Lead=george Wife=jane } 2 is { Son=bart Lead=homer Wife=marge }
How do I do this?Original AoH: 0 is { Son=bambam Lead=fred Wife=wilma} 1 is { Son=elroy Lead=george Wife=jane } 2 is { Son=bart Lead=homer Wife=marge } New AoH: 0 is { Son=bambam Lead=fred Wife=wilma} 1 is { Son=bart Lead=homer Wife=marge } 2 is { Son=elroy Lead=george Wife=jane }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to sort an array of hashes based on one of the values in the hash?
by tangent (Parson) on Mar 08, 2013 at 15:29 UTC | |
|
Re: How to sort an array of hashes based on one of the values in the hash?
by blue_cowdawg (Monsignor) on Mar 08, 2013 at 15:25 UTC | |
by tobyink (Canon) on Mar 08, 2013 at 15:49 UTC | |
by blue_cowdawg (Monsignor) on Mar 08, 2013 at 16:34 UTC |