in reply to Trouble with flow control

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.

Replies are listed 'Best First'.
Re: Re: A missleading sub
by eoin (Monk) on Jun 11, 2003 at 12:03 UTC
    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.