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, 07:49 AM   #16
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30

Yeah, I know that. What I meant was that Perl will still multiply/add/substract/etc correctly even if you don't chomp.

Change your $1 to $one, $2 to $two and so on. That will get rid of the "read-only value" error message. Then, as I said above, look at the structure of your script. You ask the user for instructions once, but you read those instructions four times. I don't want to spell it out any clearer than that.

Also, if you improve the formatting of your script (indents, blank lines between blocks, close blocks on their own line) the structure of your script will be clearer.

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

Original Poster
Rep: Reputation: 15
Ok let me give it a shot, I have never really used perl before I,m a complete newb

essentially your saying I can ask for two numbers and they will be assigned variables of $one and $two then I can just
print "$one+$two"

basically I wrote a loooong script which could be done much smaller.
when I could read those variables in for all the if statements depeding on what the user wants to do.
 
Old 03-25-2004, 08:36 AM   #18
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Yep, that's exactly right. You can call the variable (just about) anything you like: $first $second, $var1 $var2, $numA $numB, etc., etc. You'll want a \n (new line) character on the end of the print statement.

If you've got that far on your own, you're doing ok. The stuff I was saying above about the structure of the script is actually jumping ahead of your immediate problem. Fix the varnames and you'll see the next problem.

John
 
Old 03-25-2004, 08:37 AM   #19
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
OK here is my new script, cleaner and better but now I get errors?

One Syntax error at line 12 near ") print"
and the other line 34 near "elsif ("

here it is

I truly appreciate the patietence and help

Code:
print "Welcome to the Perl Calculator.\n";
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.\n";

chomp ($answer = <STDIN>)
print "please enter your first answer.\n";
($one = <STDIN>);
print "please enter a second number";
($two = <STDIN>);

if ($answer eq add){
    $one + $two;
    print "here is your answer";
}
elsif ($answer eq subtract){
    $one - $two;
    print "here is your answer";
}
elsif ($answer eq divide){
    $one / $two;
    print "here is your answer";
}
elsif ($answer eq multiply){
    $one * $two;
    print "here is your answer";
}
elsif {
    exit 1;
}
 
Old 03-25-2004, 08:48 AM   #20
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
\n has been added to the prints statements

do I need to make a variable for the answer

like

Print "$one / $two = $three"
 
Old 03-25-2004, 08:54 AM   #21
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Nice job on the formatting!

The reason for your errors...

All Perl statements must end with a semi-colon.

if and elsif statements require some condition to test.

Tips...

Do you really need that last elsif? You're going to exit anyway.

A statement that occurs identically in all of your if blocks, could be removed from them and put once before/after the if statements.

One important element missing!

John
 
Old 03-25-2004, 08:56 AM   #22
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
My reply was slower than your second question!

No, you can just

print "Your answer = ",$one+$two,"\n";

John
 
Old 03-25-2004, 09:03 AM   #23
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
I see thier is no condition test for the last elsif, its gone... now the a statement which can be removed? are you talking about the curly {}
brackets? can I put one above if and the other } after the last elsif ,I have no idea everything looks right to me. Then again it is 6am here and i,ve been workin all night =).

A glorius finals week =P
 
Old 03-25-2004, 09:14 AM   #24
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Sorry, 6am ouch! I really don't mean to make this a guessing game for you - I just think figuring things out for yourself is more valuable in the long run.

If you've added in the missing semi-colon, deleted the elsif statement around the exit and added in a print statement for the answers, then your script should be working now and you're done.

I was talking about the `print "Your answer is ";` statement. You could just have that once before the if statements and then `print $one+$two,"\n";`, etc. in each of your blocks. It was just an efficiency/style thing rather than anything essential.

Got to bed! ;->

John

Last edited by jkobrien; 03-25-2004 at 09:15 AM.
 
Old 03-25-2004, 09:44 AM   #25
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
thnanks a ton for the help

its almost 7am , got school at 1pm until, well not tryin to think about that errrrrr 10pm

time to finish my 3d animation =P

appreciate the help, have a good night or afternoon whatever time it is there in ireland.

NOOOO THE SUN!!!
 
Old 03-25-2004, 09:58 AM   #26
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
No problem, I've had plenty of help in my turn.

Here's a version that allows the user to enter the entire calculation on one line and allows decimals:

Code:
print "Enter a calculation (e.g. 4 + 56): ";
$calc = <STDIN>;

$calc =~ m/(\d+[.]?\d*)[ ]*([+*\/-])[ ]*(\d+[.]?\d*)/;
$var1 = $1;
$operator = $2;
$var2 = $3;

print "\t= ";

if ($operator eq "+") {
  print $var1 + $var2,"\n";
} elsif ($operator eq "-") {
  print $var1 - $var2,"\n";
} elsif ($operator eq "*") {
  print $var1 * $var2,"\n";
} elsif ($operator eq "/") {
  print $var1 / $var2,"\n";
} else {
  print "\n\nUnrecognised operator\n";
}

exit 1;
Of course you might find it tricky to have to explain line 4 to your prof!

John

Last edited by jkobrien; 03-25-2004 at 10:06 AM.
 
  


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 08:55 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