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);

In reply to Accessing 2nd level of hashref is failing by princepawn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.