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.
| |   | [EMAIL PROTECTED]      | I don't, and I'm not.
__/   | http://www.hjp.at/ |   -- Jesse Erlbaum on dbi-users

Attachment: pgpJzFy635Yv0.pgp
Description: PGP signature

Reply via email to