punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:
When you do
If, for any record, field_2 was empty, then $val_2 is not set, and the "print" statement throws an error "Use of uninitialized value in concatenation...". A clumsy fix would be$sql = "SELECT field_1, field_2 FROM myTable"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; $sth->execute() or die("Could not execute!" . $dbh->errstr); while (($val_1, $val_2) = $sth->fetchrow_array()) { $myhash{$val_1} = $val_2; } $sth->finish; foreach $val_1 (keys %myhash) { print "myhash{$val_1} = $myhash{$val_1}\n"; }
but there HAS to be a better way to go about it. Anyone?if ($val_2) { $myhash{$val_1} = $val_2; } else { $myhash{$val_1} = ''; }
Thanks.
|
|---|