$tree[1] = [ @{$arr[1]} ]; for (my $i=2; $i < @arr; $i+=2){ buildTree(\@tree, $arr[$i], [ @{$arr[$i+1]} ]); }