Just a short question in Perl

Greetings there foreign forum visitors. I am completely new to programming, in fact i have only started today, but i am a fast learner. I decided to undertake Perl and i wrote a simple program that has 2 main loops , each of which are supposed to take an input value number from the user, and check if it fits the requirements (being between 1 and 100 ), and then it will multiply them and find the module. Pretty straight forward. This is what i wrote:

use strict;

print "this is just a test", "\n";
MAIN_LOOP1:
print "please enter the first number between 1 and 100", "\n";
my $X = <>;
chomp($X);
for $X (1 .. 100)
{
	print "now onto the next step", "\n";
	else
	{
		print " the number does not meet the above requirment", "\n";
		next MAIN_LOOP1;
	}
}
MAIN_LOOP2:
print "please enter the second number between 1 and 100", "\n";
my $Y = <>;
chomp($Y);
for $Y (1 .. 100)
{
	print "now onto the calculations", "\n";
	else
	{
		print " the number does not meet the above requirment", "\n";
		next MAIN_LOOP2;
	}
}
print $X, " * ", $Y, " = ", ($X*$Y), "\n";
print "Finding the module" . "\n";
print $X, " % ", $Y, " = ", ($X%$Y), "\n";
print "Terminate", "\n";

And when i try to execute it in my perl cmd, i get the following errors.

c:\Strawberry\SCRIPTS>perl new.pl
syntax error at new.pl line 11, near "else"
syntax error at new.pl line 16, near "}"
syntax error at new.pl line 24, near "else"
syntax error at new.pl line 29, near "}"
Execution of new.pl aborted due to compilation errors.

Now i have searched over the forums a bit and i seemed to find other users with the same problem but none of the answers i found ultimately changed the outcome, so i thought to post my thread.

NOTE: This is my first ever program so no making fun. Also if anyone has any better syntax or recommendations, they r more than welcome.

I don't know a lot of perl , but doesn't an else require a preceding if ?

Why are you using for loops? Wouldn't a single if statement verifying that $X is less than or equal to 100 and greater than or equal to 1 be more efficient than looking for an exact match against 100 values in a loop? (At least I assume that is what you are trying to do. I don't see any comparisons in your script at all.)

The next MAIN_LOOP1; and next MAIN_LOOP2; will not work as such, you need a goto
Also, the else clause is a lost cause there.

The following examples are for demonstration, but you should stay away from goto and LABEL . It is not
a very good way of doing code in Perl. It is messy, prompt to errors and not very easy to maintain.

#!/usr/bin/perl
use strict;
use warnings;

print "This is just a test\n";
MAIN_LOOP1:
print "Please enter a FIRST number, between 1 and 100: ";
my $X = <>;
chomp $X;
goto MAIN_LOOP1 unless $X =~ /^[1-9][0-9]?$|^100$/;
print "Now, onto the next step...\n";
MAIN_LOOP2:
print "Please enter a SECOND number, between 1 and 100: ";
my $Y = <>;
chomp $Y;
goto MAIN_LOOP2 unless $Y =~ /^[1-9][0-9]?$|^100$/;
print "Now, onto the calculations...\n";
printf "$X * $Y = %d\n", $X*$Y;
print "Finding the modulus\n";
printf "$X %% $Y = %d\n", $X%$Y;
print "Test finished!\n";

Another attempt validating the number without regular expression.

#!/usr/bin/perl

use strict;
use warnings;
use Scalar::Util qw(looks_like_number);

print "This is just a test\n";
my ($X, $Y);
FIRSTN:
print "Please, enter a FIRST number, between 1 and 100: ";
# Process user input
chomp($X = <>);
# Check for a valid input
if((looks_like_number($X)) && $X >= 1 && $X <= 100) {
    # User did enter a valid input continue with test
    goto SECONDN;
}
else {
    # User did not enter valid input
    goto FIRSTN;
}
SECONDN:
print "Please, enter a SECOND number, between 1 and 100: ";
chomp($Y = <>);
# Using another way of checking that the number range is valid
# Repeat as long is not a valid number
goto SECONDN unless (looks_like_number($Y) && $Y >= 1 && $Y <= 100);
# Format evaluation using printf and not print
printf "Test for multiplication\n$X * $Y = %d\n", $X*$Y;
printf "Test for modulus\n$X %% $Y = %d\n", $X%$Y;
print "Test is finished!\n";

A cleaner and maintainable attempt.

#!/usr/bin/perl
use strict;
use warnings;

my ($X, $Y);
my @range = (1, 100);

$X = ask_number();

print "Now, onto the next step\n";

$Y = ask_number();

printf "$X * $Y = %d\n", $X * $Y;
printf "$X %% $Y = %d\n", $X % $Y;
print "Test finished!";

sub check_number {
    use Scalar::Util qw(looks_like_number);
    my $num = shift;
    looks_like_number($num) && $num >= $range[0] && $num <= $range[1];
}

sub ask_number {
    my $n;
    do {
        print "Please, enter a number, between $range[0] and $range[1], inclusive: ";
        chomp($n = <>);
    } while( not check_number($n) );
    $n;
}



1 Like

Thank you Aia