$result{1}{12}{0}? Because that's what it seems you want to do by looking at "ITEM", "Sub Item 1"... OTOH, "Item X" and "And Another Item" aren't placed after a 0 either...

Trees are a PITA, plain and simple. What you're not seeing in the example is that there's some extra logic being assumed, that makes sense to someone who's dealing with the a variable depth structure, but actually makes it more difficult to parse overall. (when it's a leaf, and not a branch, it's using different syntax -- which is annoying, when you have to add a branch there, and move the leaf)

Of course, I don't know if there's something already in place to deal with the structure. If I were starting from scratch, I'd probably use something like:

#!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; my %result = (); foreach my $line (<DATA>) { chomp $line; my ($index, $value) = split(/ /, $line, 2); my $temp = \%result; foreach my $path (split (//, $index)) { $temp = $temp->{$path} ||= {} } $temp->{0} = $value; } print Dumper \%result; __DATA__ 1 ITEM 11 Sub Item 1 111 Item X 112 Item 1121 Another Item 11212 And Another Item 12 Sub Item 2

Of course, this assumes that 0 is never used as an index, and with this structure is that the individual items need to know their path up the tree to get their whole code. If the indexes are always numbers, the following may be useful:

my %result = (); foreach my $line (<DATA>) { chomp $line; my ($index, $value) = split(/ /, $line, 2); my $temp = \%result; foreach my $path (split (//, $index)) { $temp = $temp->{$path} ||= {} } $temp->{''} = [$index, $value]; }

(and I'm surprised that tilly and I both use the same method for walking trees ... anyone else have another way to do it?)


In reply to Re^2: Generation of a Hash of Hashes by jhourcle
in thread Generation of a Hash of Hashes by Miguel

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.