svsingh has asked for the wisdom of the Perl Monks concerning the following question:
I have a script that queries a database and prints the results on an HTML page. Nothing special there. To start, I build my data structure by getting rows of data and putting them into a hash. I also have an array with all of the column headings in the query.
Originally, I merged the arrays into a hash with the following code:
while ( my @row = $dbQuery->fetchrow_array() ) { @info{@{$headings}} = @row; }
Then I realized that the null values in the table were raising undefined warnings. I fixed this by looking at each element in the row and replacing undefined values with empty strings. Here's the code:
while ( my @row = $dbQuery->fetchrow_array() ) { foreach (@{$headings}) { # replace nulls with empty strings if ( defined $row[0] ) { $info{$_} = shift @row; } else { $info{$_} = ""; } # if } # foreach } # while
I like the elegance of the former method, but it doesn't give me the data I want. The working code seems kludgy. Is there a way to get the results from the latter with the elegance of the former? How would you do this? Thank you.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Merging Arrays into a Hash with Substitutions
by Enlil (Parson) on Apr 07, 2004 at 15:52 UTC | |
Re: Merging Arrays into a Hash with Substitutions
by broquaint (Abbot) on Apr 07, 2004 at 16:00 UTC | |
Re: Merging Arrays into a Hash with Substitutions
by tilly (Archbishop) on Apr 07, 2004 at 16:22 UTC | |
by Roy Johnson (Monsignor) on Apr 07, 2004 at 16:36 UTC | |
Re: Merging Arrays into a Hash with Substitutions
by graff (Chancellor) on Apr 09, 2004 at 04:19 UTC |
Back to
Seekers of Perl Wisdom