http://qs321.pair.com?node_id=191478


in reply to do until loops

Is it possible to stop the loop if the "until" condition is never met? I am coding some user system and I use a do { for { }} until { } loop until the user name input matches one from the database. Thanks for any help.

Replies are listed 'Best First'.
Re: do until loops
by Abigail-II (Bishop) on Aug 20, 2002 at 15:40 UTC
    Frankly, your question doesn't make sense. The entire point of a do {} until loop is to repeat the loop until the condition is met. When would you stop anyway? How will you know the condition will never be met? (In general, you cannot not - that's what the halting problem is about).

    Abigail

Re: Re: do until loops
by Anonymous Monk on Sep 19, 2002 at 11:35 UTC
    Count iterations of failure, at a set count, exit..
      How to exit ?? alst is not working

        From last:

        last cannot be used to exit a block that returns a value such as eval {}, sub {}, or do {}, and should not be used to exit a grep() or map() operation.

        Note that a block by itself is semantically identical to a loop that executes once. Thus last can be used to effect an early exit out of such a block.

        So, last doesn’t work in a do { ... } until (...); loop. But — put the whole construct inside a block, and last now works fine:

        #! perl use strict; use warnings; my $n = 42; { do { printf "n = %d\n", $n--; last if $n == 37; } until ($n <= 0); } print "Past the loop\n";

        Output:

        0:22 >perl 878_Tutorial.pl n = 42 n = 41 n = 40 n = 39 n = 38 Past the loop 0:22 >

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Re: do until loops
by Anonymous Monk on Sep 09, 2003 at 23:09 UTC
    You can have a certain number of tries based on the number of values in the database: $dbnumber=66; #This would be the number of usernames. $value=1; do{ #Your input line would go here, obviously not print value. $value=$value+1; } until($value==$dbnum); #This makes it stop after checking the last value in the database.