Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2010, 02: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?
 
Old June 7th, 2010, 03:48 AM
Authorized User
 
Join Date: Aug 2009
Posts: 23
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You need chomp here.
 
Old June 7th, 2010, 04: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) ....
 
Old June 7th, 2010, 05: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
 
Old June 7th, 2010, 06: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);
The Following User Says Thank You to aurelia For This Useful Post:
c3Rhcno (June 7th, 2010)
 
Old June 7th, 2010, 05: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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Script help SMI// Javascript 3 August 30th, 2009 09:07 AM
Change the order of client script / server script rupen ASP.NET 2.0 Basics 3 April 16th, 2009 01: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 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.