in reply to Re: Hash ref assignment to array results in anomaly
in thread Hash ref assignment to array results in anomaly

What do i expect to see - When the hash ref is added as an element to the array whose key is "optional" , the elements $VAR1->{'optional'}2, $VAR1->{'optional'}2 also get added to the array. I do not understand why.
i have tried to debug the code with a whole lot of print statements , but still not managed to find out why the extra elements are added.
  • Comment on Re^2: Hash ref assignment to array results in anomaly

Replies are listed 'Best First'.
Re^3: Hash ref assignment to array results in anomaly
by GrandFather (Saint) on Jul 24, 2009 at 20:36 UTC

    As Corion has suggested, a very effective tool for finding where a problem lies is to reduce the code to just enough to see the issue. You may find some of the techniques described in I know what I mean. Why don't you? helpful for focussing on just the code you need to demonstrate the problem. In the case of your problem you might reduce the code to:

    use strict; use warnings; use Data::Dumper qw(); use Data::Dump::Streamer qw(); my %switch_param; my $switch_optional; $switch_optional->{foo} = 'bar'; my @optional; for (1 .. 3) { @optional = (@optional, $switch_optional); } $switch_param{"optional"} = [@optional]; Data::Dump::Streamer::Dump \%switch_param; print "\n\n"; print Data::Dumper::Dumper (\%switch_param);

    which prints:

    $HASH1 = { optional => [ { foo => 'bar' }, 'V: $HASH1->{optional}[0]', 'V: $HASH1->{optional}[0]' ] }; $HASH1->{optional}[1] = $HASH1->{optional}[0]; $HASH1->{optional}[2] = $HASH1->{optional}[0]; $VAR1 = { 'optional' => [ { 'foo' => 'bar' }, $VAR1->{'optional'}[0], $VAR1->{'optional'}[0] ] };

    That might, or might not, make it clear to you where the issue is, but it does make it much clearer to us not only where, but what the problem is. Note that the code uses two different data structure dumping routines which show the output somewhat differently. You may find the Streamer version more clearly illustrates the issue.

    Update added Streamer dump.


    True laziness is hard work