mdog,
Have you considered using
Tie::Hash::Sorted? It allows you to dynamically change the sort routine which it seems you want to do. Additionally, it allows you to modify the hash arbitrarily and still maintain its "sortedness".
#!/usr/bin/perl
use strict;
use warnings;
use Tie::Hash::Sorted;
my $custom_sort = sub {
my $h = shift;
[ sort {$h->{$b}{number} <=> $h->{$a}{number}} keys %$h ];
};
tie my %hash, 'Tie::Hash::Sorted', 'Sort_Routine' => $custom_sort;
my @keys = qw(name number);
@{ $hash{$_} }{ @keys } = ($_, '7.7') for qw(Me Chuck Zed);
@{ $hash{Wife} }{ @keys } = qw(Wife 7.6);
@{ $hash{Dad} }{ @keys } = qw(Dad 53);
@{ $hash{Michael} }{ @keys } = qw(Michael 24);
print "$hash{$_}{number}\t$hash{$_}{name}\n" for keys %hash;
@{ $hash{Foo} }{ @keys } = qw(foo 42);
$custom_sort = sub {
my $h = shift;
[ sort {$h->{$a}{name} cmp $h->{$b}{name}} keys %$h ];
};
tied( %hash )->Sort_Routine( $custom_sort );
print "$hash{$_}{name}\t$hash{$_}{number}\n" for keys %hash;
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.