in reply to Can Tie::IxHash be used to order elements in a list or hash of hashes?
Note that if you wanted something like an order hash of ordered hashes, you would have to tie the outermost hash and each of the inner hashes to Tie::IxHash. If only the outermost hash needs to be ordered, you only need to tie the outermost hash, of course.
Update: Here's a quick LoH example, with ordered hashes.
@LoH now contains references to 5 ordered hashes. You can treat this structure exactly like an ordinary list of hashes; plus you can use the special features of a Tie::IxHash hash whenever you want.use Tie::IxHash; my @Loh; for (1..5) { my %hash; tie %hash, 'Tie::IxHash'; push @LoH, \%hash; # a separate instance of the lexical %hash # is created each time through the loop }
This will print 'Just another Perl hacker', because Tie::IxHash returns the keys in the same order they were created. Yay!for (qw/Just another Perl hacker/) { $LoH[0]{$_} = $_; } print join(' ', keys %{ $LoH[0] }), "\n";
Many thanks to tilly for help working out a frustrating problem I ran into with my example. Upshot: do not put the tied reference itself (the return value from tie) in the data structure! Instead, tie the hash first, and put a plain old reference to the hash in the data structure.
Update: Tie::IxHash should be quoted in the call to tie. Thanks ybiC!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: Can Tie::IxHash be used to order elements in a list or hash of hashes?
by tilly (Archbishop) on May 22, 2001 at 22:39 UTC |