Just a short question in Perl


 
Thread Tools Search this Thread
Top Forums Programming Just a short question in Perl
# 1  
Old 06-13-2016
Question 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:

Code:
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.

Code:
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.
# 2  
Old 06-13-2016
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.)
# 3  
Old 06-14-2016
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.
Code:
#!/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.

Code:
#!/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.
Code:
#!/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;
}


Last edited by Aia; 06-14-2016 at 08:59 AM..
This User Gave Thanks to Aia For This Post:
# 4  
Old 06-15-2016
^_^

Thank you Aia
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl question about

Hello everybody, I am new at the forum and a total newbie when it comes to Unix. I am trying to see how I can add the ability to kill a user's processes? I want to add this to my Shel Script Add the code/process into a subroutine. Also, I would like to use an array to store the list... (0 Replies)
Discussion started by: kinelisch
0 Replies

2. Programming

Perl question

Hi, please help me with below question i am executing the below command from command line perl -e '($hi)=glob("nosum*"); print "$hi"'; As i have nosum_website.sh file in current directory it gave the following output $ perl -e '($hi)=glob("nosum*"); print "$hi"';... (2 Replies)
Discussion started by: ragilla
2 Replies

3. Shell Programming and Scripting

PERL question

Hi, we complied the perl on diff machine ( /opt/dba/perl-5.8.8) . Created TAR and using it on diff box ( again the location is same) . when I tried to put this tar to diff locaiton , it PERL connections are failing . because the @ INC still pinting to : oracle@abc:/opt/dba/oraadmin/tools DEV$... (1 Reply)
Discussion started by: talashil
1 Replies

4. Linux

short question about cron and Mailoutput

Hi Guys, short question - thereīs every 5 minutes an cronjob for user "wwwrun" - and every 5 minutes root gets an email about this cronjob. Now my question: how can i realize, that root donīt get an email about this cronjob but on all other jobs ... perhaps crontab for wwwrun: */5... (2 Replies)
Discussion started by: jackcracker
2 Replies

5. Shell Programming and Scripting

Perl Question

Hello all, I have the following line: xxx|xxxx|xxxx|xxx.xx|xxx And i insert the values in an array. I am trying to find out if the field of the array (where field=xxx.xx) has the '.' character. I am using the code below but it doesn't seem to work. if($field=~ /./) { ... (3 Replies)
Discussion started by: chriss_58
3 Replies

6. Shell Programming and Scripting

perl question

perl -wnl -e '/\bsomepattern\b/i and print $&;' I know above command only greps out the somepattern and print them(not entire line).. but how do I look for multiple pattern and only print them.. is that possible? perl -wnl -e '/\bsomepatternd\b/i and /\bsomepattern2\b/i and print $&;' does... (7 Replies)
Discussion started by: convenientstore
7 Replies

7. Shell Programming and Scripting

perl question

If I use 2 system commands in a script, will one finish before the next one starts? or will it start the first and the second at the same time? i.e. system("ps | grep rminer"); system("ls -al | grep 431"); (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

8. Shell Programming and Scripting

Perl question

Hello Everybody, I am using perl..... I have a variable called line which stores the line of a file. I dont know how many words there will be in this line. But I would like to find out in perl and also store these words in the $1,$2,$3 and $4 variables eg if the line is "first second... (2 Replies)
Discussion started by: rkap
2 Replies

9. Shell Programming and Scripting

Perl question

Hi everyone, Is Perl compiled? I keep running into references to Perl being compiled which is drivin me nuts because I thought it was an interpreted language. I am tired of the confusion, I am obviously misunderstanding something. Would someone mind explaining to me the exact order of events... (1 Reply)
Discussion started by: Jubba
1 Replies

10. Shell Programming and Scripting

PERL Question

Can anyone tell me if the copy command in PERL has the same functionality as in KSH shell in UNIX or does it actually move the file ?? $cp_stat=system("cp $ENV{OLAMEBSDIR}/data/olam.ddabal$type $ENV{OLAMDIR}/balance/data/olam.ddabal$type.$HeaderDate"); (1 Reply)
Discussion started by: frank
1 Replies
Login or Register to Ask a Question