Fw: Hash of Hashes

2002-05-07 Thread Kevin

 Kevin wrote:

  Hello,
 
  I am having a bit of a problem working with nested data structures and
after
  spending some time with the perl man pages, I am turning to the list for
  help.
 
  I am reading a DBI record into a hash reference - my $row =
  $sth-fetchrow_hashref() - I would like to create a hash to hold all of
the
  returned rows (or thus, hashes) with the ID being the key, so for
example
 
  $returned_rows{$row-{'ID'}} = $row
 
  Essentially I would have a hash of hashes, holding the result set of the
  query.
 
  My question is, how can I refer to the elements of a particular row? I
have
  attempted:
 
  $returned_rows{11}{First_Name} and it returns no results (First_Name
being a
  column).


 The easiest way to figure these things out is to dump the hash with

 Data::Dumper.


 use Data::Dumper; $Data::Dumper::Indent=1;

 print Data::Dumper-Dump([$row, $returned_rows], [qw($row
$returned_rows)]);

 That should give you the insight to figure it out.  If not, post the
 results for more help.

Bill,

Thanks for the reply. I have included the result set and some more of the
code. Your help is appreciated.


my $dbh = DBI-connect(DBI:XBase:C:/Perl/progs/customer) or die
$DBI::errstr;
my $sth = $dbh-prepare(select ID, First_Name from customer) or die
$dbh-errstr();
$sth-execute() or die $sth-errstr();
while (my $row = $sth-fetchrow_hashref()) {
$returned_rows{$row-{'ID'}} = $row;
print Data::Dumper-Dump([$row, $returned_rows], [qw($row
$returned_rows)]);
}

result:

$row = {
  'ID'= '11',
  'First_Name'= 'Tom'
  };
$returned_rows = undef;


When I attempt to put the Data::Dumper code outside of the while loop, I get
the following:

$row = undef;
$returned_rows = undef;

Thanks again.

Kevin.

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



Re: Fw: Hash of Hashes

2002-05-07 Thread $Bill Luebkert

Kevin wrote:


 Thanks for the reply. I have included the result set and some more of the
 code. Your help is appreciated.
 
 
 my $dbh = DBI-connect(DBI:XBase:C:/Perl/progs/customer) or die
 $DBI::errstr;
 my $sth = $dbh-prepare(select ID, First_Name from customer) or die
 $dbh-errstr();
 $sth-execute() or die $sth-errstr();
 while (my $row = $sth-fetchrow_hashref()) {
 $returned_rows{$row-{'ID'}} = $row;
 print Data::Dumper-Dump([$row, $returned_rows], [qw($row
 $returned_rows)]);
 }
 
 result:
 
 $row = {
   'ID'= '11',
   'First_Name'= 'Tom'
   };
 $returned_rows = undef;


my %returned_rows;  # let's predefine returned_rows


while (my $row = $sth-fetchrow_hashref()) {

$returned_rows{$row-{'ID'}} = $row;

# and fix the 2nd arg below to a ref to that hash

print Data::Dumper-Dump([$row, \%returned_rows], [qw($row %returned_rows)]);
}

# or

while ...

# do the $row here and the returned row after the loop for less output

print Data::Dumper-Dump([$row], [qw($row)]);
}

print Data::Dumper-Dump([\%returned_rows], [qw(%returned_rows)]);

-- 
   ,-/-  __  _  _ $Bill Luebkert   ICQ=14439852
  (_/   /  )// //   DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--  o // //  http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/__/_/_ Castle of Medieval Myth  Magic http://www.todbe.com/

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



Re: Fw: Hash of Hashes

2002-05-07 Thread shurst


  Thanks for the reply. I have included the result set and some more of
the
  code. Your help is appreciated.


  my $dbh = DBI-connect(DBI:XBase:C:/Perl/progs/customer) or die
  $DBI::errstr;
  my $sth = $dbh-prepare(select ID, First_Name from customer) or die
  $dbh-errstr();
  $sth-execute() or die $sth-errstr();
  while (my $row = $sth-fetchrow_hashref()) {
  $returned_rows{$row-{'ID'}} = $row;
  print Data::Dumper-Dump([$row, $returned_rows], [qw($row
  $returned_rows)]);
  }


Kevin,

$Bill got your $ vs \% problem, but  just FYI.  In general it is not safe
to store a list of references from DBI methods, many (soon to be all) of
them recycle the ref, so you could end up with an array or hash of
identical refs.


Steve


___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



Re: Fw: Hash of Hashes

2002-05-07 Thread Simon Oliver

[EMAIL PROTECTED] wrote:
 $Bill got your $ vs \% problem, but  just FYI.  In general it is not safe
 to store a list of references from DBI methods, many (soon to be all) of
 them recycle the ref, so you could end up with an array or hash of
 identical refs.
So store a copy of the data referenced by $row:

while (my $row = $sth-fetchrow_hashref()) {
 $returned_rows{$row-{'ID'}} = {%$row};

--
  Simon Oliver
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs