SPUG: while question

Peter Darley pdarley at kinesis-cem.com
Tue Sep 16 13:37:32 CDT 2003


Adrian,
	That is exactly what I was looking for!
Thanks,
Peter Darley

-----Original Message-----
From: spug-list-bounces at mail.pm.org
[mailto:spug-list-bounces at mail.pm.org]On Behalf Of Adrian Hands
Sent: Tuesday, September 16, 2003 11:25 AM
To: spug-list at mail.pm.org
Subject: Re: SPUG: while question


The real problem here is fetchrow() returns a list and you're assigning it
to a scalar.
Simply add some parens and your while loop will no longer incorrectly break
on zero.
I.e.:

 while ( ( $MyData ) = $sth->fetchrow() )


Peter Darley wrote:

> Folks,
> 	I have yet another stupid question:
>
> 	I regularly get data out of a DBI statement handle by doing:
>
> while ($MyData = $sth->fetchrow())
> {
> 	stuff
> }
>
> 	This falls down when the data returned from the statement is '0', since
> it's not true, and the while breaks.  I've tried while (defined $MyData =
> $sth->fetchrow()), but this is not syntacticly correct, and would break on
a
> database NULL (which is undef in perl) anway.  I can do something like
while
> ($MyRef = $sth->fetchrow_hashref()) {$MyData = $$MyRef{datapoint}; stuff},
> but I'm not too fond of that.  Does anyone have a suggestion on a better
way
> to construct this loop?
>
> Thanks,
> Peter Darley


_____________________________________________________________
Seattle Perl Users Group Mailing List
POST TO: spug-list at mail.pm.org  http://spugwiki.perlocity.org
ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
MEETINGS: 3rd Tuesdays, U-District, Seattle WA
WEB PAGE: http://www.seattleperl.org




More information about the spug-list mailing list