Your logic is faulty in the section which checks for 2 array elements. The problem is that you have two sections so that level of the tree happens to have 2 array elements(try print Dumper($tree)). So what happens is that your code logic enters the section name into the hash and set's it as the key with a value of undef:
if(scalar(@$aoa) == 2){ #<--------- chomp $aoa->[0]; $y=$aoa->[0]; # <-----grabs "Section1" $Config{$y}=undef; # <-------Assigns undef to it. return $key;
Hence your output. How to fix it. You will need to change your logic around. On top of that, your recursion should really pass around and return references. I've rewritten your code somewhat to traverse the tree correctly, (I made an assumption on what the resulting datastructure you wanted was):
Here's the new code:
sub deparse { my $aoa=shift; my %key; for $x (0..$#{$aoa}) { if(ref($aoa->[$x])) { %key=(%key, %{&deparse($aoa->[$x])}); } else { chomp $aoa->[0]; my $y=$aoa->[0]; if (ref($aoa->[1])) { my %C; $C{$y}=deparse($aoa->[1]); return \%C; } my %C; $C{$y}=$aoa->[1]; return \%C; } } return \%key; } __OUTPUT__ $VAR1 = { 'Section1' => { 'key2' => 'value2', 'key1' => 'value1', 'key3' => 'value3' }, 'Section2' => { 'key5' => 'value5', 'key6' => 'value6', 'key4' => 'value4' } };
HTH
In reply to Re: How do I set up the recursion?
by pzbagel
in thread How do I set up the recursion?
by TStanley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |