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

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.