What is this I don't even (control structure problems)

M

Miro

I've been using Perl for about 6 months now, and I've never had issues
with this before, but when I run this following code snippet...

my $variable = "f";
if ($variable == "d"){
print ($variable . " is d\n")
}elsif ($variable == "f"){
print ($variable ." is f\n")
}

....it says "f is d"
What am I doing wrong?
 
B

Bo Lindbergh

What am I doing wrong?

You're not using the strict and warnings pragmas. Put these two lines
at the top of your program and see if you get any interesting diagnostic
messages:

use strict;
use warnings;


/Bo Lindbergh
 
J

Jürgen Exner

Miro said:
I've been using Perl for about 6 months now, and I've never had issues
with this before, but when I run this following code snippet...

my $variable = "f";
if ($variable == "d"){

The numerical value of the content of $variable is 0. The numerical
value of "d" is also 0. Therefore the result of the numerical equality
test == is true, therefore
print ($variable . " is d\n")

this path is executed and "f is d\n" is printed.
}elsif ($variable == "f"){
print ($variable ." is f\n")
}

...it says "f is d"
What am I doing wrong?

Maybe you meant to use string comparison "eq" instead of numerical
comparison "=="?

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top