Re: Debug basic calculator syntax
by kirbyk (Friar) on Jun 23, 2005 at 21:07 UTC
|
The first thing that jumps out is the '=' inside the if clauses. You really want to use 'eq' for string comparisons and == for number comparisons. What you're doing is assigning 'y' to $add.
It also might help if you said what happened when you tried to run it. And include the !# line, so we can tell you to add a -w to it, so perl will give you extra helpful hints.
In general, this is going to raise people's "Asking for help with homework" flag, so you'll get more help if you show what you've tried to do to debug it, and maybe why you think the line that's broken should work. People like to educate those striving to learn, while are annoyed at giving people answers if they just want to avoid learning the language in the first place.
| [reply] |
Re: Debug basic calculator syntax
by CountZero (Bishop) on Jun 23, 2005 at 21:08 UTC
|
The comparison operator is not '=' but 'eq' if you want to compare strings. If you change them, the program works.Update: If you would have activated "warnings" Perl would have told you 'Found = in conditional, should be == at line 19.' which is helpful but misleading as you really should use 'eq' for string comparison.
CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] [d/l] [select] |
|
Ah...but the second run after changing '=' to '==' will give this error: 'Argument "foo" isn't numeric in numeric eq (==) at line <script name> <line number>'. It's the old theory of debugging; fix exactly one error at a time, because each error that you fix could be either creating or masking other errors.
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
| [reply] |
|
Well observed! That's why I said the warning message was helpful but misleading.
CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] |
Re: Debug basic calculator syntax
by blue_cowdawg (Monsignor) on Jun 23, 2005 at 21:04 UTC
|
use strict;
use warnings;
use diagnostics;
is your friend...
| [reply] [d/l] |
|
when i run the program, it will only do the adding part of the program
reguardless of if i enter "n"...or anything else
| [reply] |
|
perldoc perlop
is your other friend... you have lots of friends in Perl..
Kinda reminds me of the old Sunday school song
(with apologies to nobody)
"What a friend we have in Camel!"
Anyway, take a look at your comparison statement in your
first "if" statement. You are doing an
assign and not a comparison.
Check out perlop... you'll be glad you did...
| [reply] [d/l] |
Re: Debug basic calculator syntax
by GrandFather (Saint) on Jun 23, 2005 at 21:56 UTC
|
Many others have answered your immediate question. I note that your code actually would do multiplication and addition.
You may be interested in another way of doing the calculator thing. You may be interested in adding some more operators to this example.
Perl is Huffman encoded by design.
| [reply] [d/l] |
|
Although the issue at hand might be to correct the above example code, forgive me if I mention the obvious - i.e. that for evaluating formulas read in as data, the eval function does seem rather suitable. Because it is functionally overkill, a filter function is also supplied in this quickie example:
#!/usr/bin/perl
do { print "CALC> ";
printf "%f\n", eval( Filter(<>) );
} until 0;
sub Filter {
my $exp = shift;
( $exp =~ /^\s*exit/ ) and return $exp;
( $exp =~ /^\s*(\w+)/ )
and return 'print STDERR "$1 denied\n"';
return $exp;
}
example session:
CALC> 0.7*0.9
0.630000
CALC> 0.6-0.8
-0.200000
CALC> mkdir freddy;
mkdir denied
CALC> exit
$
-S
| [reply] [d/l] |
Re: Debug basic calculator syntax
by TheStudent (Scribe) on Jun 23, 2005 at 21:13 UTC
|
use strict;
use warnings;
It would have shown you immediately you have a problem with:
if ($add = "y")
and
if ($mult = "y")
Rather than using '=' it should be '=='.
| [reply] [d/l] [select] |
|
++ for advising to use strict and warnings; -- for saying to use '==' for string comparisons (it should be 'eq').
CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] [d/l] [select] |
|
I jumped to reply without thinking. The warnings from perl said:
Found = in conditional, should be == at calc.pl
Found = in conditional, should be == at calc.pl
| [reply] [d/l] |
|
| [reply] |