in reply to read flat file and build tree-like structure

Untested, but should work:

sloppy coding removed

Okay - I've learned my lesson to test before posting.

  • Comment on Re: read flat file and build tree-like structure

Replies are listed 'Best First'.
Re^2: read flat file and build tree-like structure
by ikegami (Patriarch) on Nov 19, 2007 at 17:11 UTC

    Close. Keep track of a reference to the reference.

    my %hash; while (<>) { chomp; my ($key, $val) = split(/=/, $_, 2); my @elts = split(/\./, $key); my $p = \\%hash; $p = \( ${$p}->{$_} ) for @elts; $$p = $val; } use Data::Dumper; print Dumper(\%hash);

    Tested.

      Wow, that's awesome. I'd been wondering for last few hours on the best way of doing it... thank you so much!
        Couple of follow-up questions: 1. Could you please explain what the following snippet does?
        my $p = \\%hash; $p = \(${$p}->{$_}) for @elts; $$p = $val;
        2. Is there a quick/easy way to change the key of a hash? Apologies if I'm breaking a rule here (double/cross-posting)...
Re^2: read flat file and build tree-like structure
by pc88mxer (Vicar) on Nov 19, 2007 at 17:01 UTC
    Okay - the above should work now.
      Umm... it gives you that button to "update"?