ryddler has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dereferencing nested references
by repson (Chaplain) on Jan 16, 2001 at 09:03 UTC | |
by ryddler (Monk) on Jan 16, 2001 at 19:29 UTC | |
|
Re: Dereferencing nested references
by chipmunk (Parson) on Jan 16, 2001 at 09:21 UTC | |
by ryddler (Monk) on Jan 16, 2001 at 19:50 UTC | |
|
Re: Dereferencing nested references
by eg (Friar) on Jan 16, 2001 at 09:22 UTC | |
by ryddler (Monk) on Jan 16, 2001 at 19:57 UTC | |
by jeroenes (Priest) on Jan 16, 2001 at 20:20 UTC | |
|
Re: Dereferencing nested references
by dkubb (Deacon) on Jan 16, 2001 at 09:52 UTC | |
by ryddler (Monk) on Jan 17, 2001 at 02:44 UTC | |
by btrott (Parson) on Jan 17, 2001 at 03:08 UTC |