Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Accessing 2nd level of hashref is failing

by princepawn (Parson)
on Aug 01, 2001 at 19:12 UTC ( [id://101407]=perlquestion: print w/replies, xml ) Need Help??

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

I have a fairly perplexing problem with accessing a value in a 2d hashref. Even though Data::Dumper shows that $self->{established}->{intro_rec} does exist within the hashref, attempt to access this value fail, as indicated by the two warnings (Use of uninitialized value in sprintf at blib/lib/AI/Proplog.pm line 70.) as well as by the fact that the output of the sprintf is empty. However, this is perplexing because the Data::Dumper output on the same line shows these values as existing.

There is one peculiar thing about the particular element I want to access. It was added during the second time I added things to this element of the hashref. However, a similar sample program (where I add a set of elements, then later add another set of elements) worked fine.

Source code

sub satisfied { my ($self, @body) = @_; if ($debug) { warn sprintf "TESTING @body in satisfied() with self-e: %s", Data::Dumper::Dumper($self->{established}); } for my $prop (@body) { if ($debug) { warn sprintf "prop($prop): %s dr: %s dr2: %s dump: %s", $prop, $self->{established}->{$prop}, $self->{established}->{intro_rec}, Data::Dumper::Dumper($self->{established}); }

Debug output

TESTING intro_rec comp_org adv_prog theory in satisfied() with self-e: + $VAR1 = \ { 'adv_prog' => '1', 'intro_cs' => '1', 'theory' => '1', 'comp_org' => '1', 'intro_req' => '1' }; Use of uninitialized value in sprintf at blib/lib/AI/Proplog.pm line 7 +0. Use of uninitialized value in sprintf at blib/lib/AI/Proplog.pm line 7 +0. prop(intro_rec): intro_rec dr: dr2: dump: $VAR1 = { 'adv_prog' => '1', 'intro_cs' => '1', 'theory' => '1', 'comp_org' => '1', 'intro_req' => '1' };

Attempt to reproduce the problem in simpler circumstances (works fine unfortunately)

package X; use strict; sub printlist { my ($self, @L) = @_; for my $prop (@L) { if ($self->{established}->{$prop}) { warn "$prop established"; } else { warn "$prop NOT established"; } } } my $self = {} ; bless $self, 'X'; my @L = qw(intro_cs comp_org adv_prog theory); my @M = qw(intro_req); $self->{established}->{$_}++ for @L; $self->printlist(@L); $self->{established}->{$_}++ for @M; $self->printlist(@M);

Replies are listed 'Best First'.
Re: Accessing 2nd level of hashref is failing
by nardo (Friar) on Aug 01, 2001 at 19:19 UTC
    Data::Dumper is showing intro_req exists and you're using intro_rec
Re: Accessing 2nd level of hashref is failing
by IDStewart (Acolyte) on Aug 01, 2001 at 19:25 UTC
    Your hashref contains the key intro_req, but the key you're passing in the second debug statement is intro_rec.
Re: Accessing 2nd level of hashref is failing
by princepawn (Parson) on Aug 01, 2001 at 21:35 UTC
    Thank you very much. Now I know why I hate using abbreviations.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found