Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Not meaning to add values to a hash

by stu96art (Scribe)
on Dec 21, 2012 at 16:00 UTC ( [id://1009931]=perlquestion: print w/replies, xml ) Need Help??

stu96art has asked for the wisdom of the Perl Monks concerning the following question:

I have this hash:

The values are assigned from a *.csv file, but I want to show what the structure looks like.

$flag_assignments{$key}{"starrez"} = $info[0]; $flag_assignments{$key}{"abbreviation"} = $info[1]; $flag_assignments{$key}{"greek"} = $info[2]; $flag_assignments{$key}{"Fall"} = $info[3]; $flag_assignments{$key}{"Spring"} = $info[4]; $flag_assignments{$key}{"Summer"} = $info[5]; $flag_assignments{$key}{"RA-Fall"} = $info[6]; $flag_assignments{$key}{"RA-Spring"} = $info[7]; $flag_assignments{$key}{"RA-Summer"} = $info[8]; $flag_assignments{$key}{"NR-Fall"} = $info[9]; $flag_assignments{$key}{"NR-Spring"} = $info[10];

The %starrez hash is a list of patrons in which we are deciding which of the %flag_assignments values we are assigning to them.

The hash stays as I want it (only with the keys that are assigned from the *.csv file until I get to this point in the code. I have attached the code that is adding keys to my hash below:

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 (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"; } # 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") { 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")) { if ($starrez{$key}{"RA"} ne "None") { # Patron is an RA or CRA so needs appropriate acce +ss if (exists $output{$key}) { $output{$key} = $output{$key} . ",P$flag_assig +nments{$temp_key}{$temp_semester}"; } else { $output{$key} = "C|$key|$flag_assignments{$tem +p_key}{abbreviation},(P$flag_assignments{$temp_key}{$temp_semester}"; } } else { # Patron is not an RA or CRA so will just receive +normal access if (exists $output{$key}) { $output{$key} = $output{$key} . ",P$flag_assig +nments{$temp_key}{$temp_semester}"; } else { $output{$key} = "C|$key|$flag_assignments{$tem +p_key}{abbreviation},(P$flag_assignments{$temp_key}{$temp_semester}"; } } } } 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_assignmen +ts{$temp_1[0]}{$temp_semester}"; } else { $output{$key} = "C|$key|$flag_assignments{$temp_1[ +0]}{abbreviation},(P$flag_assignments{$temp_1[0]}{$temp_semester}"; } # Find which house their Greek affiliation is in and g +ive them Non-Res (NR) access for my $key2 (sort keys %flag_assignments) { print "KEY2 [$key2]\n"; if (defined $flag_assignments{$key2}{"greek"} ne " +") { if ($temp_1[1] eq $flag_assignments{$key2}{"gr +eek"}) { my $temp2semester = "NR-" . $semester; if ($flag_assignments{$key2}{"$temp2semest +er"} ne "") { if (exists $output{$key}) { print "KEY [$key] NR-$semester [$f +lag_assignments{$key2}{$temp2semester}]\n"; $output{$key} = $output{$key} . ", +P$flag_assignments{$key2}{$temp2semester}"; } } else { print BADOUTPUT "No NR flag,$key,$star +rez{$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,$starrez +{$key}{dorm},$starrez{$key}{greek},$starrez{$key}{RA}\n"; } } } }

I am hoping to find where or how I am adding keys to the %flag_assignments hash. I didn't think that would be too tough, but I cannot find it, so I am asking for some help. Thanks.

Replies are listed 'Best First'.
Re: Not meaning to add values to a hash ("autovivification")
by Corion (Patriarch) on Dec 21, 2012 at 16:06 UTC

    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.

      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.

      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"; } } } } }
Re: Not meaning to add values to a hash
by tobyink (Canon) on Dec 21, 2012 at 16:06 UTC

    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'

      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.

Re: Not meaning to add values to a hash
by flexvault (Monsignor) on Dec 22, 2012 at 14:18 UTC

    stu96art,

    Note: These are suggestions, or just some things to think about!

    In your assignment code below (abbreviated), you need some testing for input accuracy.

    $flag_assignments{$key}{"starrez"} = $info[0]; . . . $flag_assignments{$key}{"NR-Spring"} = $info[10];
    • How about a test for the exact number of commas? If the CSV record exists but only has 3 fields, the rest of your testing is going to be wrong.
    • What if one of the fields looks like:
      ,,"Jones, Mr.",,,
      How will that affect your hash?

    Since the question about your script wouldn't be needed if the input data was 100%, try doing some reasonable verification before building the hash.

    If you are building the input data, why not build it as a hash record, i.e.

    my $record = qq|startez\t$info[0]$sep . . . |; ## $sep is a field +separator defined by you

    You can 'split' on the separator("$sep") to get the record into fields, and a simple 'foreach' loop to build the hash by 'split'ing on tab("\t"). Then you know the hash is correct. You can write this record to a file or send it to a database. Perl doesn't care how big it is, and you won't either. If fields are added, your original data will still be correct.

    If you don't have control over the input, then what will happen if one field is added in the middle of the record? Checking on the exact number of fields will at least let you know that something is wrong, and you can fix it.

    Once you verify the input, your script will do what you want.

    Good Luck...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: Not meaning to add values to a hash
by Athanasius (Archbishop) on Dec 22, 2012 at 16:29 UTC

    Hello stu96art,

    I just want to mention that an initialisation like this:

    $flag_assignments{$key}{'starrez'} = $info[ 0]; $flag_assignments{$key}{'abbreviation'} = $info[ 1]; $flag_assignments{$key}{'greek'} = $info[ 2]; $flag_assignments{$key}{'Fall'} = $info[ 3]; $flag_assignments{$key}{'Spring'} = $info[ 4]; $flag_assignments{$key}{'Summer'} = $info[ 5]; $flag_assignments{$key}{'RA-Fall'} = $info[ 6]; $flag_assignments{$key}{'RA-Spring'} = $info[ 7]; $flag_assignments{$key}{'RA-Summer'} = $info[ 8]; $flag_assignments{$key}{'NR-Fall'} = $info[ 9]; $flag_assignments{$key}{'NR-Spring'} = $info[10];

    can be written more succinctly using slices:

    my @keys = qw(starrez abbreviation greek Fall Spring Summer RA-Fall RA +-Spring RA-Summer NR-Fall NR-Spring); my %flag_assignments; @{ $flag_assignments{$key} }{ @keys } = @info[0 .. 10];

    (But flexvault’s suggestions for input validation are the better way to go.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-03-19 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found