0

I’m having a problem that seems really simple but for some reason I can’t get around it. Basically my program is causing an infinite loop and I don’t know why.

Here is the specific loop I’m getting caught in:

$response1 = false;
while($response1 == false){ 
     print "Input column #: ";
     $column = <STDIN>;
     chomp($column);
     if($column =~ m/^[0-9]+$/ and $column >= 0){ 
        $response1 = true; 
     } else {
        print "Invalid response\n";
     }  
}

When I run it, it just keeps asking me for "Input Column #". I give it a number, it accepts the number, and $response turns to True, but the while loop keeps going as if $response were false. I'm new to Perl so maybe there is something I'm missing, but doesn’t while ($response == false) indicate that if $response is to become true, the loop should terminate?

Here's the whole code for reference:

#!/usr/bin/env perl

#Input control
my $response1;
my $response2;
my $response3;
my $quit = false;

#User input
my $column;
my $row;
my $continue;

#result
my $result;

#pascal subroutine 
sub pascal{
    $r = $_[0];
    $c = $_[1];
        if($r == 0 and $c == 0){
        return 1;
    } else { 
        return (($r-$c+1)/$c)*&pascal($r,($j-1));   
    }
}

print "Pascal Triangle Calculator\n";

while($quit == false){
    $response1 = false;
    $response2 = false;
    $response3 = false;
    while($response1 == false){ 
        print "Input column #: ";
        $column = <STDIN>;
        chomp($column);
        if($column =~ m/^[0-9]+$/ and $column >= 0){ 
            $response1 = true; 
        } else {
            print "Invalid response\n";
        }   
    }
    while($response2 == false){
        print "Input row #: ";
        $row = <STDIN>;
        chomp($row);
        if($row =~ m/^[0-9]+$/ and $row >= 0){
            $response2 = true;
        } else {
            print "Invalid response\n";
        }   
    }
    $result = &pascal($row,$column);
    print "The number at row $row and column $column of the Pascal triangle is $result\n";
    while($response3 == false){
        print "Calculate another? y/n: ";
        $continue = <STDIN>;
        chomp($continue);
        if($continue == m/[yYnN]/){
            $response3 = true;
        } else {
            print "Invalid response\n";
        }   
    }    
    if($continue == m/[nN]/){
        $quit = true;
    }
}

print "Goodbye!\n";
3
  • 4
    make it work with use strict; use warnings; use diagnostics; Commented Oct 19, 2013 at 20:01
  • 2
    use strict; use warnings; will indeed show the error (and others). Always use these! Commented Oct 19, 2013 at 20:05
  • 2
    true and false are not boolean values in Perl. Without any modules or other special features loaded, they are barewords: strings, which are true in boolean context, but false 0 in numeric context ==. Commented Oct 19, 2013 at 21:00

1 Answer 1

1

As mentioned in the comments, it is always good practise to use the

use strict;
use warnings;

This will help you greatly especially when new to Perl. While the use strict will force you to tidy up the code. The issue in your code can be seen with the use warnings pragma. If i run your code with warnings i get the following output.

Argument "false" isn't numeric in numeric eq (==) at response_loop_test.pl line 4.
Argument "false" isn't numeric in numeric eq (==) at response_loop_test.pl line 4.

The == in perl is used for comparing numerical values. comparic to strings like that will not have the desired effect. Instead, you should use eq to compare for string equality.

if ($response1 eq 'false')

This will ensure the comparison of equality of the strings works as you expect. the following link describes the equality operators in perl http://perldoc.perl.org/perlop.html#Equality-Operators

Binary "==" returns true if the left argument is numerically equal to the right argument.

Binary "eq" returns true if the left argument is stringwise equal to the right argument.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.