in reply to getting out of a loop

It is hard to tell from that. Assuming you've somehow bound $x to $sth, you are printing it outside the loop not inside. The last is going to break out of the loop and goto the print statment, but $x is still defined so it will be printed.

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

More importantly though. Why arn't you letting your DB pull out the ones that start with foo and get rid of them?


___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: getting out of a loop
by o2bwise (Scribe) on Jun 16, 2006 at 20:14 UTC
    Thanks, Eric.

    Looks like we're getting into Perl DBI Mod stuff I am ignorant about. I do have the following command, just above the while loop:

    $sth->bind_columns(\$x);
    I take it, $x is "bound" and this is the reason?

    Is there a way out of this?

    The reason I haven't matched to the pattern within the sql is because I only need to see one occurrence of this in order to know what I need to know - and there may be thousands.

    I just thought it was more efficient programming to identify the first occurrence it saw and then to proceed to the other things the code needs to accomplish.

    Is there a way to work around this binding feature of the Perl DBI? (I don't know anything about it, I just adhere to the syntax for satisfying queries and such.)

    Thanks!

    Tony

      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
        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
        I learned from this code.

        Thank you.
      You might want to take a look at DBIx::Simple (plus the optional supporting libraries it can make use of). This way you can avoid some of the dirty internal details that are biting you right now.
        Thanks, Argel!

        As I wrote in my second reply to Eric, I did a workaround.

        I'll have to check that DBI::Simple.

        Tony