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

In reply to Re^3: getting out of a loop by eric256
in thread getting out of a loop by o2bwise

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.