in reply to Re^3: Strange hash array behavior
in thread Strange hash array behavior
I think the reason why I am confused is because this works and it seems to be the same concept (to me):
Update: Or an even better example of why I am confused:my $data1 = [ { "shape" => "round", "food" => "apple" }, { "shape" => "square", "food" => "pear" }, { "shape" => "oval", "food" => "grape" }, ]; my $test = $data1; my $test2 = $test; my $test3 = $test2; print Dumper($test3);
Update: And yet an even better example... why the heck does this work and not the previous examples?my $data1 = [ { "shape" => "round", "food" => "apple" }, { "shape" => "square", "food" => "pear" }, { "shape" => "oval", "food" => "grape" }, ]; foreach (0..5) { my $copy = $data1; print Dumper($copy); }
Last Update!: Using the same data above, this does not work:use Data::Dumper; my $data1 = [ { "shape" => "round", "food" => "apple" }, { "shape" => "square", "food" => "pear" }, { "shape" => "oval", "food" => "grape" }, ]; my $data2 = [ { "big" => "cow", "small" => "bunny" }, { "big" => "horse", "small" => "mouse" }, ]; foreach (0..2) { my $d = $data1->[$_]; $d->{sizes} = $data2; print Dumper($d); }
But this does, so I guess I just don't understand why it (the above) works on the first iteration but not any after. Is that really behaving like it is suppose to?my @array; foreach (0..2) { my $d = $data1->[$_]; $d->{sizes} = $data2; push(@array,$d); } print Dumper(\@array);
Thanks again for the fix!my @array; foreach (0..2) { my $d = $data1->[$_]; $d->{sizes} = $data2; push(@array,clone($d)); } print Dumper(\@array);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Strange hash array behavior
by ikegami (Patriarch) on Jan 29, 2009 at 15:26 UTC | |
|
Re^5: Strange hash array behavior
by ig (Vicar) on Jan 29, 2009 at 05:53 UTC | |
by Rodster001 (Pilgrim) on Jan 29, 2009 at 06:14 UTC | |
by Util (Priest) on Jan 29, 2009 at 07:35 UTC | |
by Rodster001 (Pilgrim) on Jan 29, 2009 at 16:22 UTC | |
|
Re^5: Strange hash array behavior
by Util (Priest) on Jan 29, 2009 at 06:27 UTC |