×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Calculate.pl Not working correctly. Please help - Newbie

Calculate.pl Not working correctly. Please help - Newbie

Calculate.pl Not working correctly. Please help - Newbie

(OP)
I have a script that allows the user to input the following:

EQUALS
CLEAR
PLUS
MINUS
OVER
TIMES

But I run the script then type PLUS 42, it actually makes the value 43.

Please help

#!/usr/bin/perl
use strict;
use warnings;

my $value;

print "\n> ";

while ( <> ) {
chomp;
if ( /^CLEAR$/ ) { $value = 0; }
elsif ( /^EQUALS$/ ) {
if ( defined $value ) {
print "\n = $value\n";
}
else { print "\n (undefined)\n"; }
}
elsif (defined $value && /^PLUS|^MINUS|^TIMES|^OVER/ ) {
$value = compute($value);
}
else {
print "\nInvalid Statement\n";
}
print "\nOK\n";
print "\n> ";
}

sub compute {
my $num = @_;
print "\nNum is: $num\n";
if ( /^PLUS\s([0-9]+)/ ) {
$num += $1;
}
elsif ( /^MINUS\s([0-9]+)/ ) {
$num -= $1;
}
elsif ( /^TIMES\s([0-9]+)/ ) {
$num *= $1;
}
elsif ( /^OVER\s([0-9]+)/ ) {
$num /= $1;
}
return $num;
}

RE: Calculate.pl Not working correctly. Please help - Newbie

Hi

CODE --> Perl

# logically wrong
my $num = @_;      # set a scalar to an array == set it to the array's length

# either correct
my $num = $_[0];   # set it to the first parameter
my $num = shift;   # extract first parameter from parameter list and set to it 

Next time please post your code between [code] and [/code] TGML tags.

Please choose an Indent style and use it consistently.

Feherke.
http://feherke.github.com/

RE: Calculate.pl Not working correctly. Please help - Newbie

(OP)
Thanks for the quick response. Sorry I didn't use

CODE

 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close