For some reason I just can't seem to wrap myself around dereferencing. Here I have a data structure I built on the fly from DB queries using the following snippet:

$SQLBuy = $dbh_metro->prepare("SELECT * FROM BuyersSellers WHERE F +ileNumber ='".$filenumber."'"); $rv = $SQLBuy->execute; while (my $hash_ref = $SQLBuy->fetchrow_hashref) { push @BuySellHashref, \$hash_ref; } $SQLBuy->finish; undef $SQLBuy; foreach (@BuySellHashref) { $SQLPEOPLE = $dbh_metro->prepare("SELECT * FROM People WHERE P +ersonID = '".${$_}->{PersonID}."'"); $rv = $SQLPEOPLE->execute; while (my $hash_ref = $SQLPEOPLE->fetchrow_hashref) { ${$_}->{PersonID} = \$hash_ref; } $SQLPEOPLE->finish; undef $SQLPEOPLE; }

After placing the hash reference in the array in the first query, it seemed to make sense to replace the value of one field (PersonID) with the data returned from the second query, because that value is the primary key of the second table, and no longer necessary after the query is performed. I figured I'd have the additional bonus of being able to only have one data structure to manipulate later on in the script. The problem I'm now facing is that what I thought was the proper dereferencing syntax

foreach $buysell (@BuySellHashref) { if (${$buysell}->{BuyerORSeller} =~ /BUYER/i) { print qq|<TR CLASS=$class><TD>$buysell->{PersonID}->{L +astName}</TD></TR>|; } }
gives me an error stating "Not a HASH reference".

I used Data::Dumper to view the structure I'm using, and it looks exactly like I figured it did, shown below:

$VAR1 = \{ 'FileNumber' => '01-0001', 'PersonID' => \{ 'FirstName' => 'JOE', 'PersonID' => '01SMITH', 'MiddleInitial' => 'D', 'LastName' => 'SMITH' }, 'BuyerSellerID' => '01-0001Bjoesmith', 'BuyerORSeller' => 'BUYER' };

"PersonID" holds a reference to a hash that conatins the data from my second query, but alas, I stumble over the dereferencing. If I want to return the value of "LastName" from the nested hash, what am I missing here?

Is there a good way to backstep out from the inside in order to build the full dereference?

ryddler


In reply to Dereferencing nested references by ryddler

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.