You never get a copy of the array referenced by $data2, not even the first time through the loop.But, it seems to work on the first iteration. util's fix solved the problem and that is the route I am going to go. This is purely academic at this point. So, my last example above (using the same data)...
But using util's solution, clone solves the problem:my @array; foreach (0..2) { my $d = $data1->[$_]; $d->{sizes} = $data2; push(@array,$d); } print Dumper(\@array); $VAR1 = [ { 'food' => 'apple', 'shape' => 'round', 'sizes' => [ { 'small' => 'bunny', 'big' => 'cow' }, { 'small' => 'mouse', 'big' => 'horse' } ] }, { 'food' => 'pear', 'shape' => 'square', 'sizes' => $VAR1->[0]{'sizes'} }, { 'food' => 'grape', 'shape' => 'oval', 'sizes' => $VAR1->[0]{'sizes'} } ];
So, why does it "work" on the first pass (without using clone) and then from there I just get a shallow copy?push(@array,clone($d));
In reply to Re^6: Strange hash array behavior
by Rodster001
in thread Strange hash array behavior
by Rodster001
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |