in reply to selectrow_hashref upper/lower case

I like to explicitly state my keys and use DBI::fetchrow_arrayref and a hash slice for better control (and a nice performance boost).

my @keys = qw/ id first_name last_name felony_convictions /; while ( my $values = $sth->fetchrow_arrayref ) { my %results; @results{ @keys } = @$values; push @data => \%results; }

With this idiom, I never have to worry about what how the DBD will handle the hash keys. Further, if you change the field names in the database, you merely update your SQL and don't have to worry about the rest of your code breaking.

If you use this, be careful that you declare your hash inside of the while loop. If you declare it outside of the loop, you'll be putting a reference to the same hash in every element of data.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Re: selectrow_hashref upper/lower case
by valdez (Monsignor) on Oct 02, 2002 at 09:22 UTC

    Thanks Ovid, I totally agree with you, but actually I must use the code already running.

    Thanks, Valerio