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

I am quite confused by this simple perl loop. I need to have a prompt to ask for a number. If the Number is higher or lower than a specific range, it will reply with a "Sorry, my bad" answer and loop back to the question. Once the number falls between the range, it will let the user out of the loop.

Here is my current attempt...

#!/usr/bin/perl $COUNT = 1; if ($COUNT = 1) { print "What is the number? "; $NUM1 = <STDIN> ; chomp ($NUM1); } elsif { $OCT1_COUNT++; } else (($NUM1 < 1) && ($NUM1 > 10)) { print "That number is not between 1 and 10.\n"; }

Original content restored above by GrandFather

Thanks for all who gave some advice. I used the following to solve my problem after I had a moment to think about it.

$COUNT = 1; while ($COUNT = 1) { print "What is the number? "; $NUM1 = <STDIN> ; if (($NUM1 < 1) || ($NUM1 > 10)) { print "That number is not between 1 and 10.\n"; } else { chomp ($NUM1); $NUM1_COUNT++; } }

Replies are listed 'Best First'.
Re: Perl script asking question and staying in a loop
by merlyn (Sage) on Oct 06, 2009 at 18:59 UTC
    if ($COUNT = 1)
    This assigns one to $COUNT, which in turn, is always true. I think you want == not =.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: Perl script asking question and staying in a loop
by toolic (Bishop) on Oct 06, 2009 at 19:01 UTC
    elsif { $OCT1_COUNT++; } else (($NUM1 < 1) && ($NUM1 > 10))
    Please post the code that you are using; this code does not compile because of syntax errors. The elsif needs a condition, and the else should not have a condition.

    Update: The OP has completely replaced the original code since I posted this response.

Re: Perl script asking question and staying in a loop
by ww (Archbishop) on Oct 06, 2009 at 22:11 UTC

    - - for replacing the original content. Please don't do that.

    Doing so leaves the replies without context (except for those who know they can look in Corion's secret stash).

Re: Perl script asking question and staying in a loop
by starX (Chaplain) on Oct 06, 2009 at 19:06 UTC
    Consider looking into IO::Interactive if you can. Alternatively, consider placing your code in a while loop:
    my $upper_limit = 10; my $lower_limit = 5; my $number; while( $number >= $lower_limit && $number <= $upper_limit) { $number = <>; }
    Although my disclaimer is that the above is untested.
Re: Perl script asking question and staying in a loop
by bichonfrise74 (Vicar) on Oct 06, 2009 at 19:29 UTC
    It looks like you just want to have a infinite loop? So, replace this
    $COUNT = 1; while ($COUNT = 1)
    with this?
    while (1)
Re: Perl script asking question and staying in a loop
by arun_kom (Monk) on Oct 06, 2009 at 19:41 UTC
    use strict; use warnings; my $lower_limit = 50; my $upper_limit = 100; my $num; print "What is the number: "; chomp($num = <STDIN>); while($num<=$lower_limit || $num>=$upper_limit){ print "\nBad answer. Try again: "; chomp($num = <STDIN>); }; print "\nThe number is: $num\n";