Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 7th, 2010, 03:09 AM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default Help with a script

I'm really new to perl, as in I started reading this book on it 2 weeks ago, so forgive me if I'm asking very basic questions.

I'm trying to get this perl calculator working and I've only just started it, here's the code for it;

Code:
#!perl
print "What would you like to do? \n.1 Add \n.2 Subtract \n.3 Multiply \n.4 Divide";
$choice = <STDIN>;
print "\n"
if ($choice = 1) {
print "What two numbers would you like to add?\n";
print "First: ";
$first = <STDIN>;
print "$first times: ";
$second = <STDIN>;
$result = $first x $second;
print "The result of $first times $second is $result";
}
My question is how do I get the if statement to know wether the user input in $choice is 1?
Reply With Quote
  #2 (permalink)  
Old June 7th, 2010, 04:48 AM
Authorized User
Points: 93, Level: 1
Points: 93, Level: 1 Points: 93, Level: 1 Points: 93, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2009
Location: Moldova
Posts: 23
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You need chomp here.
Reply With Quote
  #3 (permalink)  
Old June 7th, 2010, 05:54 AM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default where to put chomp

ok I know what chomp does and whatnot but where would I put it and why cant I have an if statement on a variable with user input, for example;

Code:
$choice = <STDIN>;
print "$choice";
if ($choice = 1) ....
Reply With Quote
  #4 (permalink)  
Old June 7th, 2010, 06:00 AM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

So I've updated my code to say
Code:
#!perl
print "What would you like to do" . "\n1 Add" . "\n2 Subtract" . "\n3 Multiply" . "\n4 Divide\n";

$choice = <STDIN>;
print "$choice\n";
if ($choice = 1) {
print "You chose to Add\n";

}

if ($choice = 2) {
print "You chose to Subtract\n";

}

if ($choice = 3) {
print "You chose to Multiply\n";

}

if ($choice = 4) {
print "You chose to Divide\n"

}
And this is what I get

http://i45.tinypic.com/2dgq5p4.jpg
Reply With Quote
  #5 (permalink)  
Old June 7th, 2010, 07:20 AM
Registered User
 
Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

In if statement you should use "==" to compare, not assign.

To receive warnings add
Code:
use warnings;
in the beginning of program, that would help to prevent some errors.

To remove "\n", You can chomp a variable after the assignment:
Code:
$choice = <STDIN>;
chomp($choice);
Reply With Quote
The Following User Says Thank You to aurelia For This Useful Post:
c3Rhcno (June 7th, 2010)
  #6 (permalink)  
Old June 7th, 2010, 06:20 PM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you for that aurelia, I needed that.
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script help SMI// Javascript 3 August 30th, 2009 10:07 AM
Change the order of client script / server script rupen ASP.NET 2.0 Basics 3 April 16th, 2009 02:41 PM
Script Help :) zere0 VBScript 6 December 7th, 2004 02:23 PM
Call and run CGI script from a PHP script ... how? dbruins BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 10th, 2003 04:09 PM



All times are GMT -4. The time now is 05:38 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.