Solved

need some help getting this simple perl program to run

Posted on 2009-05-12
5
966 Views
Last Modified: 2012-05-06
print "Rey Calculator";
print "\n";
print "Please Enter the first Number";
chomp($a = <STDIN>);
print "\n";
print "Please Enter the second Number\n";
chomp ($b = <STDIN>);
print "\n";
print "Please Enter the Operator"
chomp ($operation = <STDIN>);
print "\n";
#The following if statement assigns the command line arguments to a variable ($operation)
if ($operation eq +) {
$result = $a + $b;
}elsif ($operation eq -) {
$result = $a - $b;
}elsif ($operation eq /) {
$result = $a / $b;
}elsif ($operation eq x) {
$result = $a * $b;
}
print $a $operation $b = $result\n;


C:\>perl cal.pl
syntax error at cal.pl line 10, near "chomp"
Unrecognized character \x93 in column 19 at cal.pl line 13
0
Comment
Question by:1030071002
  • 2
  • 2
5 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 40 total points
ID: 24371589
missing ; between
print "Please Enter the Operator"  
and
chomp ($operation = <STDIN>);
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 40 total points
ID: 24371594
And what is the character between the eq and the +
if ($operation eq +) {
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 20 total points
ID: 24371643
This appears to be homework... is it?  If so, experts can help point you in the right direction, but can not do the work for you.
0
 
LVL 5

Accepted Solution

by:
vikaskhoria earned 65 total points
ID: 24371658
There are two problems in the above script:
* First is a missing semicolon at the end of line no. 9 (print "Please Enter the Operator")
* Second is you need to put all the operators in the if statements in quotes. like '+'
So your line no. 13 now becomes:
if ($operation eq '+') {

This is because you are comparing $operation as a text value.
I have debugged the code tried running it and is working fine now.
Find the corrected script below:

print "Rey Calculator";

print "\n";

print "Please Enter the first Number";

chomp($a = <STDIN>);

print "\n";

print "Please Enter the second Number\n";

chomp ($b = <STDIN>);

print "\n";

print "Please Enter the Operator";

chomp ($operation = <STDIN>);

print "\n";

#The following if statement assigns the command line arguments to a variable ($operation)

if ($operation eq '+') {

$result = $a + $b;

}elsif ($operation eq '-') {

$result = $a - $b;

}elsif ($operation eq '/') {

$result = $a / $b;

}elsif ($operation eq x'') {

$result = $a * $b;

}

print $a $operation $b = $result\n;

Open in new window

0
 
LVL 5

Assisted Solution

by:vikaskhoria
vikaskhoria earned 65 total points
ID: 24371675
One small in copy paste above.On line 20 by mistake have type x'', it should be 'x'
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PERL export multiple query results to a JSON file 1 160
crawling gofundme 4 120
Transforming a Soap message to a simple xml message! 10 144
Using Perl DBI to query oracle 3 33
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now