
rGeoffrey
User
/ Moderator
Sep 23, 2001, 5:57 PM
Post #2 of 2
(343 views)
|
|
Re: ARRAYs vs. HASHs w/ DBs..which is better
[In reply to]
|
Can't Post
|
|
When you use DBI.pm you get a set of functions...
@row_ary = $sth->fetchrow_array; $ary_ref = $sth->fetchrow_arrayref; $hash_ref = $sth->fetchrow_hashref; Which will grab the next row after you use these two lines...
my $sth = $dbh->prepare ($query); $sth->execute (); If you ask to fetch a row as an array you will get back N things. If you ask for it as a hash you get 2N things, as each thing gets a key and a value rather than just a value. If you are only grabing one or two items on each row, you are really concerned with speed, or you are not doing much processing (such as just dumping it to an html table) then the array solution is the way to go. But if you are trying to remember what a particular item in the array is, or you are doing interesting stuff to the results from each row, it may be easier to work with as a hash. But it is a tradeoff of speed for understanding. In this case you would use them as...
while (my @row_ary = $sth->fetchrow_array ()) { print $row_ary[0]; }while (my $ary_ref = $sth->fetchrow_arrayref()) { print $ary_ref->[0]; }while (my $hash_ref = $sth->fetchrow_hashref ()) { print $hash_ref->{key1}; }
|