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.