Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Can't quit my until loop

by ellem (Hermit)
on Jul 03, 2001 at 19:28 UTC ( [id://93541]=perlquestion: print w/replies, xml ) Need Help??

ellem has asked for the wisdom of the Perl Monks concerning the following question:

I have this code:

#!/usr/bin/perl -w use strict ; use diagnostics; use LWP::Simple ; print "This will get the time from an NIST clock 5 times and then ask you if you want to continue. Use 'y' to continue and any other key to stop:\n\n" ; my $answer ; my $quit = 0 ; until ($quit) { getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; print "Do you want to continue? " ; $answer = <STDIN> ; chomp $answer ; } if ($answer ne 'y') { print "OK\n" ; } else { $quit = 1 ; die "OK!\n\n" ; } print "Good-bye!\n\n" ;


Basically it never quits. I assume this is because $quit never becomes true.

I don't see where I have gone astray and I have compared my code (specifically the until ($quit) to $quit = 1 part. I obviously don't understand looping (see I'm not even sure if that's the right word!) Any advice would be helpful.

--
lmoran@wtsgSPAM.com
print "\x{263a}"

Replies are listed 'Best First'.
(ichimunki) Re: Can't quit my until loop
by ichimunki (Priest) on Jul 03, 2001 at 19:36 UTC
    Your formatting and your {} block structure do not match. The last line of your until loop is chomp $answer. Remove the } from in front of the next if and all should be well again.

    Update:You'll also need to add a closing brace after your else block. Suggested form:
    until ($COND) { some($stuff); if ($THIS) { do($that); } else { do($anything); } }
      Got it! I had been moving things all over the place tyring to get it to work. It was the way I had setup the block. Thanks for your clearer explanation of the until construct.

      anylou... this works.
      #!/usr/bin/perl -w use strict ; use diagnostics; use LWP::Simple ; print "This will get the time from an NIST clock 5 times and then ask you if you want to continue. Use 'y' to continue and any other key to stop:\n\n" ; my $answer ; my $quit = 0 ; until ($quit) { getprint "http://132.163.4.101:13" ; getprint "http://132.163.4.101:13" ; getprint "http://132.163.4.101:13" ; getprint "http://132.163.4.101:13" ; getprint "http://132.163.4.101:13" ; print "\n\nDo you want to continue? " ; $answer = <STDIN> ; chomp $answer ; if ($answer eq 'y') { print "OK\n" ; } else { $quit = 1 ; } } print "Good-bye!\n\n" ;


      --
      lmoran@wtsgSPAM.com
      print "\x{263a}"
        You may still want to look at how you are using your braces relative to your indenting structure (see the addition to my note and the other comments). A good text editor like emacs (among others) will do a lot of this work for you.
Re: Can't quit my until loop
by Hofmator (Curate) on Jul 03, 2001 at 19:42 UTC

    Probably you see it yourself if I modify the layout of your code:

    my $answer ; my $quit = 0 ; until ($quit) { getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; print "Do you want to continue? " ; $answer = <STDIN> ; chomp $answer ; } if ($answer ne 'y') { print "OK\n" ; } else { $quit = 1 ; die "OK!\n\n" ; } print "Good-bye!\n\n" ;
    You are never altering $quit in the loop ... so it can't terminate ;-)

    -- Hofmator

Re: Can't quit my until loop
by tachyon (Chancellor) on Jul 03, 2001 at 19:55 UTC

    Consider your code with some indentation added

    #!/usr/bin/perl -w use strict ; use diagnostics; use LWP::Simple ; print "This will get the time from an NIST clock 5 times and then ask you if you want to continue. Use 'y' to continue and any other key to stop:\n\n" ; my $answer ; my $quit = 0 ; until ($quit) { getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; print "Do you want to continue? " ; $answer = <STDIN> ; chomp $answer ; } if ($answer ne 'y') { print "OK\n" ; } else { $quit = 1 ; die "OK!\n\n" ; } print "Good-bye!\n\n" ;

    I have indented your code as I normally would. The problem as you can see is that you test the conditional outside the until loop. No test == No exit. You could also simplify a lot to this:

    #!/usr/bin/perl -w use strict ; use diagnostics; use LWP::Simple ; print "This will get the time from an NIST clock 5 times and then ask you if you want to continue. Use 'y' to continue and any other key to stop:\n\n" ; my $answer = 'y' ; until ($answer !~ /^y$/i ) { getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; print "Do you want to continue? " ; $answer = <STDIN>; chomp $answer; } print "Good-bye!\n\n" ;

    I like this structure a lot more as it is simple and skips extra vars. Less code == Less bugs. By using a regex match we allow a little more flexibility with lower case or capital 'Y' acceptable.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Something completely different (was "Re: Can't quit my until loop")
by rrwo (Friar) on Jul 03, 2001 at 20:22 UTC

    On a completely different track (since everybody else answered this question)... looking at the following:

    if ($answer ne 'y') { print "OK\n" ; } else { $quit = 1 ; die "OK!\n\n" ; }

    Why not try the following:

    $quit = ($answer !~ m/^y/i);

    This way if somebody enteres "y" or "Y" or "Yes" etc. it will work (never underestimate what users will type in... even yourself).

    The die is not necessary since the until loop is fixed. You're breaking program structure by forcing an exit with die.

(dkubb) Re: (2) Can't quit my until loop
by dkubb (Deacon) on Jul 03, 2001 at 20:29 UTC

    You can also use what's called a naked block, to do what you want:

    #!/usr/bin/perl -w use strict ; use LWP::Simple qw(getprint); use constant NIST_SERVER => 'http://132.163.4.101:13'; print <<HERE; This will get the time from an NIST clock 5 times and then ask you if you want to continue. Use 'y' to continue and any other key to stop: HERE { getprint(NIST_SERVER) for 1..5; print 'Do you want to continue? [yn]: '; chomp(my $answer = <STDIN>); redo if lc($answer) eq 'y'; } print "Good-bye!\n\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found