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

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 apologise for posting the whole code. I thought that probably some other part of the code was effecting such behavior.

  • 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 Corion (Patriarch) on Jul 24, 2009 at 12:22 UTC

    The problem stems, as I already described, from your top-level declaration of $switch_optional. You're not initializing it there, so Perl initializes it to a hash reference when you first use it as a hash reference. As you never change the value of the reference, Perl keeps that value. You then push, in the loop, that reference onto your array. So Perl pushes that same value of the same reference to your array. Three times in total.

    If you reduce your program to the relevant part, you actually find out what parts are irrelevant and what parts are relevant. This is an important capability to have or to evolve, if you want to have any chance at succeeding to program. So please, show the effort and reduce your program to only the relevant parts.