Hello monks. I am trying to convert following structure (even elements are "parents" and odd are "children"):

$VAR1 = 'ng1'; $VAR2 = [ 'ng1_1', 'ng1_2', 'ng1_3', 'ng1_4' ]; $VAR3 = 'ng2'; $VAR4 = [ 'ng2_1', 'ng2_2', 'ng2_3', 'ng2_4' ]; $VAR5 = 'ng3'; $VAR6 = [ 'ng3_1', 'ng3_2', 'ng3_3', 'ng3_4' ]; $VAR7 = 'ng1_1'; $VAR8 = [ 'ng1_1_1', 'ng1_1_2', 'ng1_1_3', 'ng1_1_4' ]; $VAR9 = 'ng1_1_1'; $VAR10 = [ 'ng1_1_1_u1', 'ng1_1_1_u2', 'ng1_1_1_u3' ]; $VAR11 = 'ng2_1'; $VAR12 = [ 'ng2_1_u1', 'ng2_1_u2', 'ng2_1_u3' ];

to tree structure which will looks like this:
$VAR1 = 'ng1'; $VAR2 = [ 'ng1_1', [ 'ng1_1_1', [ 'ng1_1_1_u1', 'ng1_1_1_u2', 'ng1_1_1_u3' ], 'ng1_1_2', 'ng1_1_3', 'ng1_1_4' ], 'ng1_2', 'ng1_3', 'ng1_4' ]; $VAR3 = 'ng2'; $VAR4 = [ 'ng2_1', [ 'ng2_1_u1', 'ng2_1_u2', 'ng2_1_u3' ], 'ng2_2', 'ng2_3', 'ng2_4' ]; $VAR3 = 'ng3'; $VAR4 = [ 'ng3_1', 'ng3_2', 'ng3_3', 'ng3_4' ];
But after "for loop" I noticed that @arr has changed for unknown reasons, to this:

$VAR1 = 'ng1'; $VAR2 = [ 'ng1_1', [ 'ng1_1_1', [ 'ng1_1_1_u1', 'ng1_1_1_u2', 'ng1_1_1_u3' ], 'ng1_1_2', 'ng1_1_3', 'ng1_1_4' ], 'ng1_2', 'ng1_3', 'ng1_4' ]; $VAR3 = 'ng2'; $VAR4 = [ 'ng2_1', 'ng2_2', 'ng2_3', 'ng2_4' ]; $VAR5 = 'ng3'; $VAR6 = [ 'ng3_1', 'ng3_2', 'ng3_3', 'ng3_4' ]; $VAR7 = 'ng1_1'; $VAR8 = $VAR2->[1]; $VAR9 = 'ng1_1_1'; $VAR10 = $VAR2->[1][1]; $VAR11 = 'ng2_1'; $VAR12 = [ 'ng2_1_u1', 'ng2_1_u2', 'ng2_1_u3' ];

Can somebody please explain me why is this happening? Code which I am using for this is following (there is only one for loop for debug purposes). Maybe this is not optimal code, any recommendations are welcomed.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @arr = ( 'ng1', ['ng1_1','ng1_2', 'ng1_3', 'ng1_4'], 'ng2', ['ng2_1','ng2_2', 'ng2_3', 'ng2_4'], 'ng3', ['ng3_1','ng3_2', 'ng3_3', 'ng3_4'], 'ng1_1', ['ng1_1_1','ng1_1_2', 'ng1_1_3', 'ng1_1_4'], 'ng1_1_1', ['ng1_1_1_u1', 'ng1_1_1_u2', 'ng1_1_1_u3'], 'ng2_1', ['ng2_1_u1', 'ng2_1_u2', 'ng2_1_u3'] ); my @tree; #print "\nBEFORE CALLING FIRST FOR LOOP\n"; #print Dumper @arr; $tree[0] = $arr[0]; $tree[1] = $arr[1]; for (my $i=2; $i < @arr; $i+=2){ &buildTree(\@tree, $arr[$i], $arr[$i+1]); } #print "\nAFTER CALLING FIRST FOR LOOP\n"; #print Dumper @arr; #$tree[2] = $arr[2]; #$tree[3] = $arr[3]; #for (my $i=4; $i < @arr; $i+=2){ # &buildTree(\@tree, $arr[$i], $arr[$i+1]); #} sub buildTree{ my ($tree, $parNg, $subNg) = @_; for my $treeElement (@{$tree}){ if (ref $treeElement eq "ARRAY"){ &buildTree($treeElement, $parNg, $subNg); } else{ if ($treeElement eq $parNg){ my ($index) = grep { $tree->[$_] eq $treeElement } 0..scal +ar(@$tree)-1; splice @{$tree}, $index + 1, 0, $subNg; } } } }

Thank you

In reply to Convert array to tree OR why variable changes arbitrarily by Anonymous Monk

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.