note
bart
You're <em>not</em> creating the nested hash in the proper order, as you're using anonymous hashes as values somewhere, which are intrinsically unordered. And then, it's too late to fix it.
<p>First off, take a look at a module our co-monk [japhy] wrote: [module://Tie::Autotie]. It'll automatically tie deeper hashes also. If you then would assign the values one by one, it would already work:
<c>
use Tie::Autotie 'Tie::IxHash';
tie %hash_ref, 'Tie::IxHash'; # yuck, that variable's name!
$hash_ref{MAIN}{ZOP}{Dev} = undef;
$hash_ref{MAIN}{ZOP}{Con} = undef;
... # etc
</c>
<P>You might not like the required syntax — though for reading from a data file, it'd probably work fine. I'm thinking of an alternative to anonymous hashes: tied hash references. I don't know if any module already implements them, but you can build them by hand:
<c>
sub ixhash {
tie my(%hash), 'Tie::IxHash';
%hash = @_;
return \%hash;
}
</c>
In that case, you should be able to write (including the above sub):
<c>
use Tie::IxHash;
tie %hash_ref, 'Tie::IxHash';
%hash_ref = (
'MAIN' => ixhash(
'ZOP' =>
ixhash(
'Dev', undef,
'Con', undef,
'Test', undef,
'Exit', undef,
'New', undef ),
'AP' =>
ixhash(
'Dev', undef,
'Con', undef,
'Test', undef,
'Exit', undef,
'New', undef ),
'Exit' => undef,
)
);
</c>
which looks acceptable to me... no?
<p><u>update</u> n.b. You don't need to use [cpan://Tie::Autotie] if you use the latter suggestion, make sure to replace every anonymous hash with a call to ixhash(), and never rely on autovivification.
<p>p.s. You can use [module://Tie::Hash::Indexed] as a plug-in replacement for [module://Tie::IxHash]. The former is written in XS, the latter in Pure Perl, so it <em>should</em> be faster. I haven't run a benchmark, though.
551214
551214