in reply to Re: Re: Re: appending an array to a hash
in thread appending an array to a hash

Here's another alternative:

$sth->bind_col(1, \my $key); my %rows; while(my $row = $sth->fetchrow_hashref) { $rows{ $key } = $row; }

With this version you don't need to maintain a list of columns, and it will automatically make the first column the key in %rows. It's probably more efficient/faster to use $sth->fetchrow_hashref than doing a foreach since $row is made using a hash slice.

Replies are listed 'Best First'.
Re: (dkubb) Re: (5) appending an array to a hash
by Anonymous Monk on Mar 23, 2001 at 01:19 UTC
    the only examples of hashes i've seen in any of the books seem to demonstrate that the key can only reference one piece of data. whereas in the table i have i need to use the round number to reference the team name and the score for that row. I also return multiple rows from the sql selection step so i need to take each row one at a time and add them to the hash as a row of three pices of data - the first being the key. what does the putting of the "my $row" inside the while step do? what is the syntax to reference the first column in row one. what is the syntax to reference second column in row one. i'm new to perl so i might seem a bit of an amateur.