SPUG: while question

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


Folks,
	Thanks for everyone's suggestions.  It looks like the consensus is that its
best to get back a reference so the while() doesn't check the value of
what's returned, just that there's something there.
	The only reason I don't like getting the reference is that I want to make
sure that if I have a single value, it's in a scalar.  If there is a hash or
an array it should hold multiple values.  This keeps the application
stylistically consistent and easier to go through at some later date.
	I guess I'll go with while ($Ref = $sth->fetchrow) {$MyValue = $$Ref[1];
stuff}
	Thanks for everyone's input! :)
Thanks,
Peter Darley

-----Original Message-----
From: Terry Nightingale [mailto:tnight at pobox.com]
Sent: Tuesday, September 16, 2003 10:42 AM
To: Peter Darley
Cc: SPUG
Subject: Re: SPUG: while question


Peter,

I personally would use either $sth->fetchrow_hashref() or
$sth->fetchrow_arrayref() in exactly the manner you describe below.  The
nice thing about getting a reference to the data rather than just the
data itself is exactly what you're running up against.  Even if you have
a reference to '0' or undef, the reference itself is both defined and
true, so your loop works correctly.

Hope that helps,
Terry


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