in reply to Creating Hash of hash of hash dynamically
The core loop to do something like this is actually pretty simple. Let's say you have an array of level names, a target value and hash to start from:
#! /usr/bin/perl -w # Set up some stuff for this example use strict; use Data::Dumper; my %hash; my @levels = qw(a b c); my $value = 5; # The next three lines are the essence: my $work = \\%hash; $work = \$$work->{$_} for @levels; $$work = $value; # Show that it worked print Dumper(\%hash);
If the number of levels can be 0, the top level assign can be a scalar one, so you'd need to start with a scalar instead of a hash:
my $hash; my $work = \$hash; $work = \$$work->{$_} for @levels; $$work = $value;
The advantage of this forward walking way of doing things is that you can apply it several times to put multiple level/value sets in there (as long as you take care that a value doesn't block a subtree)
However, if there is some character that cannot appear in the titles, it's probably easier to just join all titles using that character as separator and use that as hash key. You can use split if you need to split up such a key into titles again. This leads to a much easier to use and understand datastructure (if there is no such character, you can still get this idea working with some form of escaping or counted packs, but it gets a bit trickier and maybe not worth it anymore), e.g.:
In your case the input already has the comma separated titles as a substring, which you could even extract directly after reading a line.$hash{join(",", @levels)} = $value;
|
|---|