note
samtregar
It's a little tricky, but not hard once you've seen it done once:
<p>
<code> my %hash;
my $value = "foo";
my @cats = qw(a b c d);
my $p = \%hash;
foreach my $item (@cats) {
$p->{$item} ||= {};
$p = $p->{$item};
}
$p->{_val} = $value;
use Data::Dumper;
print Dumper(\%hash);
</code>
<p>
Output:
<p>
<code>$VAR1 = {
'a' => {
'b' => {
'c' => {
'd' => {
'_val' => 'foo'
}
}
}
}
};
</code>
<p>
The trick here is to keep a pointer ($p) to the insertion-point for the next category as you walk through the structure. You also need some way to distinguish values from categories - in this case I used a special "_val" key.
<p>
-sam
747210
747210