Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Not meaning to add values to a hash

by tobyink (Canon)
on Dec 21, 2012 at 16:06 UTC ( [id://1009933]=note: print w/replies, xml ) Need Help??


in reply to Not meaning to add values to a hash

I'm not entirely sure what the question is, but you may be experiencing autovivification. Here's an example:

use Data::Dumper; my %empty_hash = (); if (exists $empty_hash{foo}) { print "'foo' exists\n"; } if (exists $empty_hash{foo}{bar}) { print "'foo'->'bar' exists\n"; } print Dumper \%empty_hash; if (exists $empty_hash{foo}) { print "Oh look, now 'foo' exists\n"; }

The autovivification module on CPAN can be used to disable autovivification for particular blocks of code where it's causing issues.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Not meaning to add values to a hash
by stu96art (Scribe) on Dec 21, 2012 at 16:56 UTC

    Yes, it looks like that is exactly what I am experiencing.

    I guess that I am looking at this in a novice way, but I thought that in your code, if $empty_hash{foo}{bar} did not exist, then print "'foo'->'bar' exists\n"; would not be executed.

    Would a way around this be to only check if the key in the hash existed and not the element itself? Example:

    if (exists $empty_hash{foo}) {

    Instead of:

    if (exists $empty_hash{foo}{bar}) {

      I thought that in your code, if $empty_hash{foo}{bar} did not exist, then print "'foo'->'bar' exists\n"; would not be executed.

      Did you, in fact, try to run the code? You'll see that that print statement indeed will not be executed.

      As for the workaround you suggest, no. Checking whether a certain key exists in a hash is not a workaround for checking whether a subkey exists somewhere deeper down you structure. Obviously if the key doesn't exist, this implies that the subkey doesn't exist, but if the key existst this tells us nothing about the existance of the subkey. Proof:

      use strict; use warnings; use Data::Dumper; # Some arbitrary data my %earth = (); $earth{wind}{fire} = "water"; # Show me what we've got print Dumper \%earth; print "\n\n"; print "\nEarth has wind, wind has fire.\n" if exists $earth{wind}->{f +ire}; delete $earth{wind}{fire}; print "The fire is extinguised.\n" unless exists $earth{wind} +->{fire}; print "Yet earth still has wind.\n" if exists $earth{wind}; delete $earth{wind}; print "But the wind blows away.\n" unless exists $earth{wind} +; print Dumper \%earth; print "\n\n"; print "Relight that fire!\n" if exists $earth{wind}{fir +e}; # Autovivication happens here! print Dumper \%earth; print "\n\n";
      $VAR1 = { 'wind' => { 'fire' => 'water' } }; Earth has wind, wind has fire. The fire is extinguised. Yet earth still has wind. But the wind blows away. $VAR1 = {}; $VAR1 = { 'wind' => {} };

      The earliest moment where you autovivify elements in %flag_assignments is the print statement in this snippet:

      if (defined $flag_assignments{$temp_key}) { print "KEY [$key] TK [$temp_key] TS [$temp_semester] FAS [$fla +g_assignments{$temp_key}{starrez}] FAA [$flag_assignments{$temp_key}{ +abbreviation}]\n"; }
      And you have many more statements like that where you autovivicate in string interpolation. This is not in itself a bad thing, but it explains the behaviour you are seeing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1009933]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-03-19 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found