Re: [OT] Warnings from code when using perl-dbi



On 2007-08-16 18:30:52 +0800, Ow Mun Heng wrote:
On Thu, 2007-08-16 at 11:50 +0200, Peter J. Holzer wrote:
You really want to know whether $first[$counter] is undef, not whether
it has a length == 0 here, so you should test for that:

if (!defined($first[$counter]))

That worked.. I was thinking of checking the length because if there the
data is NULL, then length should be 0 and not undef.

In SQL, length(NULL) is NULL, not 0. In Perl length(undef) is 0, but you
get a warning.


Here's the altered one, for brewity..

my @first;
while ( @first = $sth->fetchrow_array )
{
my $count = @first;
my $first = @first;

for ($counter = 0; $counter < $count; $counter = $counter + 1)
{
if ($counter == $count-1)
{
if (!defined($first[$counter]))
{
# WHAT DO I PUT HERE?

Nothing. There is nothing to do in this case.

} else {
print "\"$first[$counter]\"";
}

You could invert the test to avoid the empty if:

if (defined($first[$counter]))
{
print "\"$first[$counter]\"";
}

[...]

Of course the whole loop can be written quite a bit shorter (and more
"perlish"):

print join(q{,}, map { defined $_ ? qq{"$_"} : q{} } @first), "\n";

hp

--
_ | Peter J. Holzer | If I wanted to be "academically correct",
|_|_) | Sysadmin WSR | I'd be programming in Java.
| | | hjp@xxxxxxxxx | I don't, and I'm not.
__/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users

Attachment: pgpFwcExLcZuc.pgp
Description: PGP signature



Relevant Pages

  • Re: Noob Questions [arrays]
    ... A loop that is not a .each loop that reads in user input ... until the input is an empty line ... puts 'type words one at a time and I will alphabetize them for you.' ... to the empty string before entering the while loop. ...
    (comp.lang.ruby)
  • HELP! how do i read in multiline data blocks?
    ... fgetl, etc., and trying out various things, but I am not succeeding. ... ONE HEADER LINE ... ONE EMPTY LINE ... I know I want a loop that goes through one datablock at a time, ...
    (comp.soft-sys.matlab)
  • Re: Copy row with an empty cell for each row in a sheet
    ... loop throgh every row until it reachs the end of the worksheet ... if one of the cells is empty it will copy this row to a new sheet ...
    (microsoft.public.excel.misc)
  • Re: Noob Questions [arrays]
    ... A loop that is not a .each loop that reads in user input ... until the input is an empty line ... puts 'type words one at a time and I will alphabetize them for you.' ... the user submits a 0 length string. ...
    (comp.lang.ruby)
  • Re: Copy row with an empty cell for each row in a sheet
    ... loop throgh every row until it reachs the end of the worksheet ... if one of the cells is empty it will copy this row to a new sheet ...
    (microsoft.public.excel.misc)