Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

do until loops

by root (Monk)
on Nov 05, 1999 at 04:59 UTC ( [id://951]=perltutorial: print w/replies, xml ) Need Help??

These work the same as do while loops only they execute until the condition evaluated is true
Here's a quick example:
$value=5;
do{
  print "$value\n";
  $value=$value-1;
} until($value<=10);


This is executes once and prints out $value;
5
Then $value is decremented and becomes 4
since 4<=10 is true execution of the loop is terminated

Now onto the exciting world of for loops

Replies are listed 'Best First'.
Re: do until loops
by Anonymous Monk on Jul 07, 2001 at 01:27 UTC
    I'm trying to do a simple copy command that will overwrite a file a specific number of times(i.e 10 times and then terminate) using the do until command. Can anyone give me some advise on this.
      couldnt you do something like this
      $value=0; do{ "copy command" $value=$value+1; } until($value==9);
      #!/usr/bin/perl -w< use strict;<br> # use the -w and "use strict" # to check more on your errors # usefull thing for rookies my $content = "some content"; # here asuming that the file is in the same folder my $filename = "file_name.file_extention"; # i like the while loop better..... my $stopper = 0; # loop until $stopper equal 10 # opening the file for overwrite or die # print new data # close file # incress $stopper value by one while ($stopper < 10) { open yourfile, ">$filename" or die "\t cant open file"; print yourfile "$content"; close yourfile; $stopper++; } # done # if u relly want to use do until # replace my while loop from { to } by .. do { open yourfile, ">$filename" or die "\t cant open file"; print yourfile "$content"; close yourfile; $stopper++; } until ($stopper == 10) # # but a question is in my mind why the *~@ u wanna # overwrite a file 10 times ?????? # anyway hope i helped u a little
      Edit by dws to add <code> tag
Re: do until loops
by Anonymous Monk on Aug 20, 2002 at 15:23 UTC
    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.
      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

      Count iterations of failure, at a set count, exit..
        How to exit ?? alst is not working
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (9)
As of 2024-03-28 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found