Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Trouble with flow control

by eoin (Monk)
on Jun 10, 2003 at 16:43 UTC ( [id://264754]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,
This script was one of my very first, made late last year. Since then I have come on leaps and bounds mainly thanks to you all here. I thought that I had lost both the script and the formula for this equation but I found it just lying in a forgotten undisturbed part of hard drive. I spruced it up a small bit and hope to integrate tk's graph module into it but i seem to have a problem....

The script runs fine i.e. Perl gives no errors, but when I get to the sub confirm part thats where I have problems.

Initially it should,
if $answer is equal to "n" send you back to the start
if $answer is equal to "y" send you to the middle sub
and if $answer is equal to anything else send you back to the top of confirm.

It doesn't.

No matter what I type. I always end up going back to the start subroutine. And I'm starting to feel a bit lost. Going round in circles if you will! Any and all comments are as always very welcome. Thank you in advance.
Heres the code:
#To determine the pattern of fish reproducing #in a pond #Using the Chaos Theory &start(); ############################################################ sub start{ print "Welcome to the 'Fish in a Pond' chaos theory solution guide +.\n" . "Please enter the number of fish in the pond on the f +irst year:\n"; print "\n"; $nof = <STDIN>; print "\n"; print "Please enter the anual rate of increase: \n"; print "\n"; $roi = <STDIN>; print "\n"; print "For what period of time would you like the simulation t +o run for(in years): \n"; print "\n"; $yrs = <STDIN>; chomp($nof); chomp($roi); chomp($yrs); &confirm(); } ############################################################ sub middle{ $ans = $nof; $year = 0; print "Year $year. = $ans fish."; print "\n"; &calc(); } ############################################################ sub calc{ while($year <= $yrs) { $year = $year + 1; $i = $ans * $roi; $x = 1 - $ans; $ans = $i * $x; print "Year $year. = $ans fish. \n "; } } ############################################################ sub confirm{ print "The data that you have entered says: \n" . " * that there is $nof in the pond on the fisrt year. \n" . " * that the annual rate of increase is $roi. \n" . " * that the simulation will run for $yrs years. \n"; print " Is this information correct?(Y/N): "; $answer = <STDIN>; chomp($answer); if ($answer = "n") { &start(); } elsif($answer = "y") { &middle(); } else{ print "Please enter either Y or N. \n"; &confirm(); } } ############################################################

All the Best, Eoin...

If everything seems to be going well, you obviously don't know what the hell is going on.

Edit by tye, add READMORE

Replies are listed 'Best First'.
Re: A missleading sub
by Mr. Muskrat (Canon) on Jun 10, 2003 at 16:51 UTC

    The problem lies here:

    if ($answer = "n") { &start(); } elsif($answer = "y") {
    = is used for assignment. == and eq are used for equality testing of numbers and strings respectively. Also, you will want to make your code case insensitive.

    The corrected code is:

    $answer = lc($answer); if ($answer eq "n") { &start(); } elsif($answer eq "y") {

    Another alternative altogether would be to check $answer with a regex.

      Silly me. Sorry this is what happens when you spend to long sitting in front a screen. Thanks
      All the Best, Eoin...

      If everything seems to be going well, you obviously don't know what the hell is going on.

Re: A missleading sub
by Not_a_Number (Prior) on Jun 10, 2003 at 18:46 UTC

    If you'd had warnings on, perl would have told you what you were doing wrong.

    Always:

    use strict; use warnings;
    dave
Re: A missleading sub
by LAI (Hermit) on Jun 10, 2003 at 16:52 UTC

    Silly eoin, you're using = instead of eq

    here:

    if ($answer = "n")

    Update: Okay, so Mr. Muskrat beat me to the punch :o)

    LAI

    __END__
Re: A missleading sub
by shemp (Deacon) on Jun 10, 2003 at 16:57 UTC
    You have problems in your logic to decide what to do. A single equal sign "=" is used for assignment, so when you write
    if ( $answer = "n" )
    what is really happening is you are assigning "n" to the variable answer. The value of the assignment is the thing being assigned, namely "n", which evaluates to true, so the if condition is satisfied.

    What you need is string comparison, in this case "eq". "eq" evaluates to true is the strings are equal, false otherwise. So, what you probably want is:
    if ( $answer eq "n" ) {
    This still wont catch the case where the user enters "N" (capitalized). There are many ways to deal with this, i prefer:
    if ( uc($answer) eq "N" ) {
    This will uppercase the value of $answer, so then you only need to check it against the capital "N".

    Your caveat was twofold:
    1)using an assignment operator when you should be using a comparison operator.
    2)using a numeric operator when a string operator is called for.

    for #1, you just need to be careful.
    for #2, the easiest way to remember what to use is: when doing numeric comparisons, use the normal math symbols (<, >, =, etc), and when doing string comparisons, use the comparators that are letters (eq, ne, lt, gt, etc)

    One final note, you dont need to use the "&" to denote a subroutine call anymore. you can just say
    confirm()
    hope this helps.
      I've fixed all the problems that I can see but it still throws me the wrong way. Now what ever I enter in the confirm sub activates the else tag of the if statement. And sends me back to confirm()!!! Heres what I've done: (I ran it with warnings on.)
      #To determine the pattern of fish reproducing #in a pond start(); sub start{ print "Welcome to the 'Fish in a Pond' chaos theory solution guide +.\n" . "Please enter the number of fish in the pond on the f +irst year:\n"; print "\n"; $nof = <STDIN>; print "\n"; print "Please enter the anual rate of increase: \n"; print "\n"; $roi = <STDIN>; print "\n"; print "For what period of time would you like the simulation t +o run for(in years): \n"; print "\n"; $yrs = <STDIN>; chomp($nof); chomp($roi); chomp($yrs); $confirm = confirm(); if ($confirm eq 'n') { start(); } elsif($confirm eq 'y') { middle(); } else{ print "Please enter either Y or N. \n"; confirm(); } } sub middle{ $ans = $nof; $year = 0; print "Year $year. = $ans fish."; print "\n"; calc(); } sub calc{ while($year <= $yrs) { $year = $year + 1; $i = $ans * $roi; $x = 1 - $ans; $ans = $i * $x; print "Year $year. = $ans fish. \n "; } } sub confirm{ print "The data that you have entered says: \n" . " * that there is $nof thousand fish in the pond on the fisrt +year.\n" . " * that the annual rate of increase is $roi. \n" . + " * that the simulation will run for $yrs years. \n\n"; print "Is this information correct?(Y/N): "; $answer = <STDIN>; $answer =lc($answer); return($answer); }
      Help please.
      All the Best, Eoin...

      If everything seems to be going well, you obviously don't know what the hell is going on.

Re: A missleading sub
by Enlil (Parson) on Jun 10, 2003 at 16:52 UTC
    In your conditionals you probably want eq instead of = (for which you probably meant to have == anyhow.)
    • = assigns
    • == compares numbers
    • eq compares strings.

    -enlil

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found