in reply to Convert array to tree OR why variable changes arbitrarily
This is quite a confusing, but interesting question. The idea is to build a second structure @tree that is a restructured version of @arr. @arr is assumed to be left as is?
The problem is that you are working with references to arrays, your are not copying the underlying arrays when you build @tree. So your line splice @{tree} ... is really inserting parts of @arr into the new variable and operate on them. If you replace this line by creating a copy of @$subNg and insert it into tree you should be fine (hopefully).
As a minor comment: I prefer to use Dumper \@arr as this creates a single structure and is more readable IMHO.
Proposed changes to leave @arr untouched:
$tree[0] = $arr[0]; my @copy = @{$arr[1]}; $tree[1] = \@copy; ... my ($index) = grep { $tree->[$_] eq $treeElement } 0..scalar(@$tre +e)-1; my @copy = @$subNg; splice @{$tree}, $index + 1, 0, \@copy;
Whether or not this modified code creates the desired @tree I do not know.
|
|---|