I'll ignore the question if this is actually a good idea here (what happens if you want to put a value in a place that's also needed for a deeper part of the tree ?).

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.:

$hash{join(",", @levels)} = $value;
In your case the input already has the comma separated titles as a substring, which you could even extract directly after reading a line.

In reply to Re: Creating Hash of hash of hash dynamically by thospel
in thread Creating Hash of hash of hash dynamically by rjc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.