### Illogical logic statement

by fkento (Novice)
 on Dec 01, 2012 at 02:23 UTC Need Help??
fkento has asked for the wisdom of the Perl Monks concerning the following question:

I can't for the life of me figure this one out:
my \$str = 'false'; if ( \$str == 'true' ) { print "\$str=true\n"; } else { print "\$str=false\n"; }
Output:

false=true

What am I missing?

Replies are listed 'Best First'.
Re: Illogical logic statement
by toolic (Bishop) on Dec 01, 2012 at 03:09 UTC
What am I missing?

Tip #1 from the Basic debugging checklist: warnings

Argument "true" isn't numeric in numeric eq (==) at line 11 (#1) (W numeric) The indicated string was fed as an argument to an oper +ator that expected a numeric value instead. If you're fortunate the me +ssage will identify which operator was so unfortunate. Argument "false" isn't numeric in numeric eq (==) at line 11 (#1) false=true
Re: Illogical logic statement
by Athanasius (Chancellor) on Dec 01, 2012 at 02:32 UTC

Hello fkento, and welcome to the Monastery!

From Truth and Falsehood:

The number 0, the strings '0' and "", the empty list (), and undef are all false in a boolean context. All other values are true.

So, since the stringfalse” is none of 0, '0', the empty string, the empty list, or undef, it is true in Perl.

Update: Sorry, misread the question. The answer lies in the distinction between the operators == and eq. The former is for numerical comparison, the latter for string equality. See Equality Operators.

12:43 >perl -E "say (('false' == 'true') ? 'same' : 'different');" same 12:43 >perl -E "say (('false' eq 'true') ? 'same' : 'different');" different 12:43 >

Hope that helps,

Athanasius <°(((><contra mundum

Thanks Athanasius. But I'm just trying to compare two strings. Here is a simpler case:
if ( 'f' == 't' ) { print "f equals t\n"; } else { print "f does not equal t\n"; }
Output:

f equals t

How does one compare strings?
> How does one compare strings?

Cheers Rolf

Compare '15' eq '15.0' versus '15' == '15.0'! Or if this looks artificial, let's assume you've read one of the numbers from a XML file and the other from STDIN. Now both are stored as strings by Perl, strings that will be converted to numbers if needed, but still what you read from both sources were strings! How should Perl guess whether you wanted to compare them as strings or as numbers? And if Perl did try to guess, how would you make sure it guesses right?

A single comparison operator is fine for strongly typed languages, but for weakly typed like Perl and JavaScript a pair of operators works much better. Actually the shared operator for string and number comparison and another for addition and concatenation is what I dislike most about JavaScript and what leads to huge loads of errors in JavaScript code in general. formFieldName.value + 1 anyone?

Jenda
Enoch was right!
Enjoy the last years of Rome.

Try this:

if ( 'f' eq 't' ) { print "f equals t\n"; } else { print "f does not equal t\n"; }
Re: Illogical logic statement
by roboticus (Chancellor) on Dec 01, 2012 at 02:37 UTC

Nearly all values are true. False is reserved for a select few values like 0, '0' and ''.

Roboticus@Roboticus-PC ~ \$ perl t.pl 0 is false 0 is false foo is true bar is true fkento is true true is true false is true yes is true no is true is false Roboticus@Roboticus-PC ~ \$ cat t.pl #!/usr/bin/perl use strict; use warnings; for my \$t (0, qw(0 foo bar fkento true false yes no), '') { print "\$t is ", (\$t ? 'true' : 'false'), "\n"; }

Remember--text strings are just strings, even if they say "false" or "true". You should read the perldata (third paragraph after 'scalar' heading) for the basics.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Re: Illogical logic statement
by GrandFather (Sage) on Dec 02, 2012 at 21:16 UTC

When I execute:

#!/usr/bin/perl use strict; use warnings; my \$str = 'false'; if (\$str == 'true') { print "\$str=true\n"; } else { print "\$str=false\n"; }

I get:

Argument "true" isn't numeric in numeric eq (==) at noname.pl line 7. false=true Argument "false" isn't numeric in numeric eq (==) at noname.pl line 7.

which is a pretty good heads up! Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

True laziness is hard work

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006555]
Approved by Athanasius
Front-paged by 2teez
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-02-25 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (314 votes). Check out past polls.

Notices?