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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.