in reply to Re^2: getting out of a loop
in thread getting out of a loop

Actualy you are probably still better off putting it in your SQL. Most database servers have an exists keyword, or if the value you are querying on is keyed then it will be VERY fast.

Either way you don't want to leave the loop when it happens and then do what you want. Because then you don't know if you left the loop because you hit the end or because you found what you where looking for. If you want to use the loop, execute specific code if you find a match, then stop looking at the results you want the following:

while ( $sth->fetch() ) { chomp $x; if ( $x =~ /^foo/ ) { print "$x\n"; last; } }

Keep in mind that the database is still retrieving all the results so this is probably not as good as letting the DB handle the comparison.

my ($count) = $dbh->selectrow_array("SELECT count(*) FROM table WHERE +LEFT(colum,3) = 'foo'"); if ($count > 0) { print "Do what you need here"; }

___________
Eric Hodges

Replies are listed 'Best First'.
Re^4: getting out of a loop
by o2bwise (Scribe) on Jun 16, 2006 at 21:49 UTC
    Well, I did a workaround. I changed my query to select count (*) of the column where it is like 'foo%' and then after the query asked if count was > 0.

    Thanks, both of you!

    Oh, and I didn't know the db performed the full query anyway.

    Tony
Re^4: getting out of a loop
by o2bwise (Scribe) on Jun 17, 2006 at 01:40 UTC
    I learned from this code.

    Thank you.