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