argv has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to build a hierarchical hash that reflects a string syntax that represents parent-child relationships. E.g.:
my $str = "One:Two:Three:four, One:Two:3:4, One:2:3:4, One:2:three";
should result in
$keywords = { One => { Two => { Three => four, 3 => 4 }, 2 => { 3 => 4, three => "" } } };
Though it should go without explanation, the idea is that "One" is the parent node to everything. It has two children, "Two" and "2". Each of those has children, who are either leaves or have their own children....
My current implementation is to split each comma-delimeted token along the :'s, and then call itself recursively to build the hash. I'm sooooo close, but it's just not getting it. I don't want to post my code so far so as to avoid influencing a better, more efficient method... you know how it goes: you work on something too long, and you can't see the forest for the trees anymore, and you doubt whether you went about it right in the first place.
The routine to test whether it works is here:
my $n = 0; sub print_keywords { my $k = shift; for my $key (keys %$k) { print " " x $n; print "$key\n"; $n++; if (keys %{$k->{$key}}) { print_keywords($k->{$key}); } elsif ($k->{$key}) { print " " x $n, $k->{$key}, "\n"; } $n--; } }
And, of course, you call it with the top of the hash returned after the parsing is done:
print_keywords($keywords);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parsing a parent-child syntax to make a hash
by Fletch (Bishop) on Jun 08, 2007 at 17:56 UTC | |
|
Re: parsing a parent-child syntax to make a hash
by ikegami (Patriarch) on Jun 08, 2007 at 18:46 UTC |