http://qs321.pair.com?node_id=11113723


in reply to RESOLVED - Autovivification: How Did I End Up With a Larger Number of Hash Keys Here?

Hi, I would think that if (exists $auto_picks{$data[0]}{$data[1]}) has the potential to autovivify keys that did not exist. autovivification ...

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: How Do I End Up With a Larger Number of Hash Keys Here?
by perldigious (Priest) on Mar 03, 2020 at 19:41 UTC

    Helps a lot, that's the problem I'm having.

    https://perlmaven.com/autovivification

    Also, good God, why?

    I'd be more inclined to call that a bug than a feature, especially in the exists/delete context I'm doing above.

    I'm both amazed I haven't run in to this prior, and terrified that maybe I have in past code I've written and I just never realized it. :-o

    Just another Perl hooker - My clients appreciate that I keep my code clean but my comments dirty.
      Its probably to late, but I do want to point out that your original code would work as intended (and perhaps even be clearer) if you test the levels separately. The "short circuit" feature of the "and" operator prevents the troublesome low-level test from running unless the upper-level key exists.
      if ( exists $auto_picks{ $data[0] } and exists $auto_picks{ $data[0] }{ $data[1] } ) {
      Bill
Re^2: How Do I End Up With a Larger Number of Hash Keys Here?
by talexb (Chancellor) on Mar 03, 2020 at 19:05 UTC

    Uh .. no.

    tab@music4:~ $ perl -de 1 Loading DB routines from perl5db.pl version 1.51 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(-e:1): 1 DB<1> %hash = (); DB<2> use Data::Dumper; DB<3> print Dumper(\%hash) $VAR1 = {}; DB<4> if ( exists $hash{nothing} ) { print "Wow.\n"; } DB<5> !3 print Dumper(\%hash) $VAR1 = {}; DB<6>
    For me, the whole point of using exists is to avoid doing autovivification.

    But the example used a two level hash .. so I answered the wrong question. Ugh.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      Uh .. no.

      Actually, yes. 1nickt had a HoH, not just a simple hash. Intervening levels can and do get created, even when called by exists

      > perl -de 1 Loading DB routines from perl5db.pl version 1.27 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> %hoh = (); DB<2> use Data::Dumper; DB<3> print Dumper(\%hoh) $VAR1 = {}; DB<4> if (exists($hoh{nothing}{second})) { print "wow.\n"; } DB<5> !3 print Dumper(\%hoh) $VAR1 = { 'nothing' => {} };