Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Looped around my neck (help with my poor little loops!)

by sulfericacid (Deacon)
on Mar 15, 2003 at 20:30 UTC ( [id://243340]=perlquestion: print w/replies, xml ) Need Help??

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

My next venture on my geekhood is LOOPS!! Dang those stupid things anyways, lol, all they do is ruin my chances of getting more difficult scripts to work :) I tried following help in the CB about going back to the earlier loop and to put this variable into who-knows-where but maybe if someone has time they can show me a pseudo-code.

I don't want anyone to fix this script, that's sorta what I want to achieve otherwise I can't call it my own. Can someone show me the right method to do say something like:

if (($a != b) && ($error > 0 && $error <= 3 )) { repeat question and <STDIN> increment $error++ } if ($a == $b) { print "Correct!\n" print "Only took you $error times!\n"; } if ($error >= 3) { print "Took you too many tries, you lose\n"; }
That is the jist of what I am trying to do with my ugly, ugly script below.

#!/usr/bin/perl use strict; use warnings; print "Enter the max number:\n"; chomp(my $maxguess = <STDIN>); $maxguess =~s/\r//; my $range = rand($maxguess + 1)%10; my $numguess = 0; my $tries = 3; print "What is your guess?\n"; chomp(my $guess = <STDIN>); $guess =~s/\r//; $numguess++; # print "Guess: $guess\n"; # print "Range: $range\n"; until ($numguess > 0 && $numguess >= $tries) { #print "this should work\n"; &guessing; $numguess++; if ($numguess >= $maxguess) { print "You lose!\n"; } } sub guessing{ while ($guess != $range){ print "Wrong answer! Please try again!\n"; print "Guess: \n"; chomp(my $guess = <STDIN>); print "Guess #: $numguess\n"; print "Max # : $maxguess\n"; print "Answer : $range\n\n\n\n"; if ($guess == $range) { print "You win!\n"; exit; } last; } } if ($numguess >= $maxguess) { print "You lose!\n"; } elsif ($guess == $range) { print "You win!\n"; print "It only took you $numguess time(s)!!\n"; }

Again, I'm just looking on how to get the loops setup, I don't need anyone debugging the script (Gadz knows that would take you weeks). Thanks so much for your help!



"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Looped around my neck (help with my poor little loops!)
by Enlil (Parson) on Mar 15, 2003 at 20:42 UTC
    For all your looping needs, look at perlsyn. There are several there which you should easily be able to modify into what you need.

    -enlil

      Wow, that link does look useful! Thanks Enlil :) *is going to run out of paper from printing so many docs* hehe

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        Don't forget that perldoc is a friend to you and the trees ;-) From a command line you can get loads and loads of information:

        perldoc perlsyn

        I've managed to spend hours staring at the screen, reading various perldoc entries. Very handy tool!


        I just realized that I was using the same sig for nearly three years.

Re: Looped around my neck (help with my poor little loops!)
by poj (Abbot) on Mar 15, 2003 at 23:23 UTC
    Psedo code
    set $max_tries to 3. ask user for $range of guess generate $answer as a random number from 1..$range FOR $loop from 1 to $max_tries $guess_count = $loop get $user_guess if $user_guess is the $answer then exit loop ENDFOR IF $user_guess equals the $answer message : Well done, you got it in $guess_count ELSE message : Sorry, you've had $max_tries guesses ENDIF
    poj
      If the way you wrote it works you definately make me think I wrote the whole thing backwards, in reverse and in some non-textual language, lol. The only thing I don't understand is what you mean by:

      FOR $loop from 1 to $max_tries $guess_count = $loop
      Are you saying I should do a:

      for ( increment # of guesses to $max_tries) { # no clue what the 2nd line means then
      ? Thanks so much for your help, looks like I totally globbed this script up.

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        I found that when you jump out of the loop with last you lose the value of the $loop variable
        If you need it later like in the message you need to store it with $guess_count=$loop, that's all it does
        poj
Re: Looped around my neck (help with my poor little loops!)
by pfaut (Priest) on Mar 15, 2003 at 20:43 UTC

    The loop in &guessing is not really a loop at all. It will either exit (bad form to exit from a subroutine) or execute the last statement on the first pass. I think that while really wants to be an if.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      bad form to exit from a subroutine

      Baloney! Programs should exit wherever it makes sense to exit.
      Sometimes — yea, most of the time — that is in a subroutine.

      (Update)

      For fun, see the Single Function Exit Point anti-pattern.

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.

Log In?
Username:
Password:

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

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

    No recent polls found