LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-25-2004, 04:11 AM   #1
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Rep: Reputation: 15
Whats wrong with this PERL script?


This is a basic perl script I wrote thats supposed to be a very simple calculator but I keep getting error msg's .

I guess my first question is can I have that many inputs and if so how do I format them so they read in separate variables, is their a better way of doing it??

The errors I get are lines 16, near "chomp"
line 20 near "elsif"
line 24 near "chomp"
line 25 near ";}"

I,m sure there is probably more, if you see them please point them out
here is my code
Code:
print "please enter the function you would like me to calculate, but 
keep it simple please, I may be a talking calculator but my functions are 
somewhat limited, choose from, add, subtract, divide, multiply, or 
raising to a power.\n";

chomp ($add = <STDIN>);
if ($add eq "add"){
	print "please enter your first Number.\n";
	chomp ($1 = <STDIN>);
	print "please enter your second variable.\n"
	chomp ($2 = <STDIN>);
print "$1+$2";
print "above is your answer";
	chomp ($subtract = <STDIN>);
elsif ($subtract eq "subtract"){
	print "please enter your first Number.\n";
	chomp ($3 = <STDIN>);
	print "please enter your second variable.\n"
	chomp ($4 = <STDIN>);
print "$3-$4";}
	chomp ($divide = <STDIN>);
elsif ($divide eq "divide"){
	print "please enter your first Number.\n";
	chomp ($5 = <STDIN>);
	print "please enter your what you want to divide the first number 
by.\n"
	chomp ($6 = <STDIN>);
print "$5/$6";}
	chomp ($multiply = <STDIN>);
elsif ($multiply eq "multiply"){
	print "please enter your first Number.\n";
	chomp ($7 = <STDIN>);
	print "please enter your second variable.\n"
	chomp ($8 = <STDIN>);
print "$7*$8";}
 
Old 03-25-2004, 04:48 AM   #2
dominant
Member
 
Registered: Jan 2004
Posts: 409

Rep: Reputation: 30
You forgot the }
 
Old 03-25-2004, 04:55 AM   #3
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Punctuation marks.

You're missing two semi-colons and one right curly bracket. Also you have a statement between the end of one elsif and the start of another.

Go find 'em!

John
 
Old 03-25-2004, 05:16 AM   #4
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
ok I found a couple ; and } but I still get an error on line 20 near the elsif
says its a syntax error, I see none???
 
Old 03-25-2004, 05:27 AM   #5
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Your second elsif block has become detached from rest of your if statement. I.e. the same as if you had no "if" statement and just launched into "elsif".

It should be

if () {} elsif () {} elsif () {}

You have

if () {} elsif () {}
statement
elsif () {}

John

Last edited by jkobrien; 03-25-2004 at 05:28 AM.
 
Old 03-25-2004, 05:32 AM   #6
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
I,m not following what your saying john. DO I just need to redo that statement?? How did only the second one become unattached?
 
Old 03-25-2004, 05:34 AM   #7
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
AHHH I get it I cant have statements between the elsifs, I need to do separate if statements for all of them? Is that what your getting at?
 
Old 03-25-2004, 05:41 AM   #8
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Bingo!

I haven't analysed your program just looked at syntax. But, yeah, either put that statement in the previous elsif block or start a new if statement.

John
 
Old 03-25-2004, 05:42 AM   #9
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
If i do separate IF's for all of them it works, BUT when it comes time to input the variables it says

Modification of read-only value attempted at ./perlcalc line 14, <STDIN>
Line 1

Cant I have more than one input/variable in a if statement?? Is there another way to do this??
 
Old 03-25-2004, 06:10 AM   #10
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
I think you need to look at the structure of your script. You have four input statements for the operation to be performed.

John
 
Old 03-25-2004, 06:31 AM   #11
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
Yes but I need them to be variables, is there a way I can have numerous variables with out using the <STDIN> command??? I is there another way to ask the user for numbers and have PERL input them into variables?? I only learned <STDIN> to make variables
 
Old 03-25-2004, 06:46 AM   #12
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Ok, sorry, what I said above is still true but having looked more closely, you're using reserved variable names. A variable can't be called 1, or 2, etc.

John

p.s. not strictly true, there are variables, $1, $2, etc., but they are already assigned to a specific function.

Last edited by jkobrien; 03-25-2004 at 06:51 AM.
 
Old 03-25-2004, 06:57 AM   #13
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
Ahhh, You see this is my first scripting class and, we briefly touched on PERL

I wanted to learn it a little better so I chose to do some bash scripts in perl for my final, I didnt know $1 nd $2 were reserved variables in PERL and had a specific meaning.

Do you know how to input numerous variables other than by
Code:
chomp ($whatever = <STDIN>)
 
Old 03-25-2004, 07:07 AM   #14
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
No, apart from reading them from a file. I don't think the chomp is strictly necessary, though.

John
 
Old 03-25-2004, 07:41 AM   #15
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
chomp just removes the newline, you can also
$whatever = <STDIN>

I just need to know how to make it work, guess I,ll look at some tutorials or somethin.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats wrong with my dialing script? pen^2 Linux - General 13 09-16-2004 03:34 AM
Whats wrong with this script??? Sammy2ooo Linux - Networking 2 08-24-2004 07:20 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
Whats wrong with this? OlRoy Programming 2 05-28-2003 02:21 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 06:03 AM


All times are GMT -5. The time now is 04:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration