Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Not meaning to add values to a hash ("autovivification")

by Corion (Pope)
on Dec 21, 2012 at 16:06 UTC ( #1009932=note: print w/ replies, xml ) Need Help??


in reply to Not meaning to add values to a hash

You access complex structures with %flag_assignments as the root. Perl will helpfully bring intermediate elements into life automatically ("autovivify") if they don't exist.

In the following code, if $flag_assignments{$temp_key} does not exist, it will get autovivified due to its use a few lines later:

... 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"; } # Here it is possible that $flag_assignments{$temp_key} does not e +xist # ONLY ONE FLAGE PER CATEGORY, AND GREEKS LIVING ON CAMPUS WILL AL +SO GET NR ACCESS TO THE HOUSE OF THE FRAT/SORORITY if ($starrez{$key}{"dorm"} ne "None") { # Here, autovivification of a value for $flag_assignments{$tem +p_key} happens # because it's part of $flag_assignments{$temp_key}{$temp_seme +ster} if ($flag_assignments{$temp_key}{$temp_semester} ne "") { ...

One approach to prevent autovivification is to use exists checks before accessing a complex data structure. Another could be to use Data::Diver. To prevent accidential autovivification, see autovivification.


Comment on Re: Not meaning to add values to a hash ("autovivification")
Select or Download Code
Re^2: Not meaning to add values to a hash ("autovivification")
by stu96art (Scribe) on Dec 21, 2012 at 16:44 UTC

    Thanks. I had used 'exists' before I tried 'defined' with the same results.

    What I am trying to accomplish is to only reference the element in the hash if it actually exists. I had also tried to do an if statement where the hash element would have to equal the correct key, but I received a multitude of "Uninitialized value...".

      If one side of the comparison is uninitialized (due to not existing), then Perl warns.

Re^2: Not meaning to add values to a hash ("autovivification")
by stu96art (Scribe) on Dec 21, 2012 at 17:29 UTC

    Thanks for all of the help. I have made adjustments and without using the autovivification module, I have the code working. I will admit, that it is not as elegant as I would like for it to be, but for now it works. Thanks.

    for my $key (sort keys %starrez) { if (exists $starrez{$key}{"dorm"}) { } else { $starrez{$key}{"dorm"} = "None"; } if (exists $starrez{$key}{"greek"}) { } else { $starrez{$key}{"greek"} = "None"; } if (exists $starrez{$key}{"RA"}) { } else { $starrez{$key}{"RA"} = "None"; } if (exists $explanation{$key}) { } else { $explanation{$key} = "None"; } my $temp_key = $starrez{$key}{"dorm"}; if ($starrez{$key}{"greek"} ne "None") { $temp_key = $temp_key . "\|$starrez{$key}{greek}"; } my $temp_semester = $semester; if ($starrez{$key}{"RA"} ne "None") { $temp_semester = "RA\-" . $temp_semester; } if (exists $flag_assignments{$temp_key}{"starrez"}) { if (exists $flag_assignments{$temp_key}{"abbreviation"}) { if (($flag_assignments{$temp_key}{"starrez"} ne "") and ($ +flag_assignments{$temp_key}{"abbreviation"} ne "")) { #print "KEY [$key] TK [$temp_key] TS [$temp_semester] +FAS [$flag_assignments{$temp_key}{starrez}] FAA [$flag_assignments{$t +emp_key}{abbreviation}]\n"; } } } # ONLY ONE FLAG PER CATEGORY, AND GREEKS LIVING ON CAMPUS WILL ALS +O GET NR ACCESS TO THE HOUSE OF THE FRAT/SORORITY if ($starrez{$key}{"dorm"} ne "None") { if (exists $flag_assignments{$temp_key}{$temp_semester}) { if ($flag_assignments{$temp_key}{$temp_semester} ne "") { if (($flag_assignments{$temp_key}{$temp_semester} ne " +x") and ($flag_assignments{$temp_key}{$temp_semester} ne "None")) { print "PIK [$key] TK [$temp_key] TS [$temp_semeste +r] FA [$flag_assignments{$temp_key}{$temp_semester}]\n"; if ($starrez{$key}{"RA"} ne "None") { # Patron is an RA or CRA so needs appropriate +access if (exists $output{$key}) { $output{$key} = $output{$key} . ",P$flag_a +ssignments{$temp_key}{$temp_semester}"; } else { $output{$key} = "C|$key|$flag_assignments{ +$temp_key}{abbreviation}|(P$flag_assignments{$temp_key}{$temp_semeste +r}"; } } else { # Patron is not an RA or CRA so will just rece +ive normal access if (exists $output{$key}) { $output{$key} = $output{$key} . ",P$flag_a +ssignments{$temp_key}{$temp_semester}"; } else { $output{$key} = "C|$key|$flag_assignments{ +$temp_key}{abbreviation},(P$flag_assignments{$temp_key}{$temp_semeste +r}"; } } } } else { #print "TK [$temp_key]\n"; if ($temp_key =~ '|') { my @temp_1 = split('|',$temp_key); # Give access to their dorm if (exists $output{$key}) { $output{$key} = $output{$key} . ",P$flag_assig +nments{$temp_1[0]}{$temp_semester}"; } else { $output{$key} = "C|$key|$flag_assignments{$tem +p_1[0]}{abbreviation}|(P$flag_assignments{$temp_1[0]}{$temp_semester} +"; } # Find which house their Greek affiliation is in a +nd give them Non-Res (NR) access for my $key2 (sort keys %flag_assignments) { print "KEY2 [$key2]\n"; if (exists $flag_assignments{$key2}{"greek"}) +{ if ($flag_assignments{$key2}{"greek"} ne " +") { if ($temp_1[1] eq $flag_assignments{$k +ey2}{"greek"}) { my $temp2semester = "NR-" . $semes +ter; if ($flag_assignments{$key2}{"$tem +p2semester"} ne "") { if (exists $output{$key}) { print "KEY [$key] NR-$seme +ster [$flag_assignments{$key2}{$temp2semester}]\n"; $output{$key} = $output{$k +ey} . ",P$flag_assignments{$key2}{$temp2semester}"; } } else { print BADOUTPUT "No NR flag,$k +ey,$starrez{$key}{dorm},$starrez{$key}{greek},$starrez{$key}{RA}\n"; } } } } else { #print "TEMP_KEY [$temp_key]\n"; } } } else { print BADOUTPUT "Not Greek and should be,$key,$sta +rrez{$key}{dorm},$starrez{$key}{greek},$starrez{$key}{RA}\n"; } } } } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-12-29 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls