[Nh-pm] The Perly Way
Ray Cote
rgacote at AppropriateSolutions.com
Wed May 14 11:47:19 CDT 2003
Hi Folks:
The following piece of code works perfectly fine, but I'm thinking
there's probably a more Perlish solution to the problem.
As you can see from the code fragment, the runQuery routine is
executing an SQL statement and all the results are returned in an
array of arrays.
In order to print these nicely, I then convert any Oracle null values
into the string 'NULL'.
My solution was to iterate (using a map) through each array and then
iterate (using map) through each element of that array.
Is there a simpler way to do this?
One that would iterate through all the elements in one pass?
Thanks
--Ray (perl 'prentice at play)
# check and 'NULL' a single element.
# this is the inner map
sub fixupNULL {
my ($s) = @_;
$s = 'NULL' if !defined($s);
return $s
}
# grab a row at a time out of the array.
# this is the outer map
sub fixupNULLrow {
my ($aref) = @_;
@$aref = map fixupNULL($_), @$aref;
return $aref;
}
# run query and output result with Oracle nulls converted to string 'NULL'.
sub runQuery {
my ($sql) = @_;
my $aref = dbSqlReturnAllRows( $dbh, $sql ) or die;
my $rowref;
@$aref = map fixupNULLrow($_), @$aref;
foreach $rowref (@$aref)
{
print join( "\t", @$rowref) . "\n";
}
}
--
Raymond Cote
Appropriate Solutions, Inc.
PO Box 458 ~ Peterborough, NH 03458-0458
Phone: 603.924.6079 ~ Fax: 603.924.8668
rgacote(at)AppropriateSolutions.com
www.AppropriateSolutions.com
More information about the Nh-pm
mailing list