Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Illogical logic statement

by Athanasius (Monsignor)
on Dec 01, 2012 at 02:32 UTC ( #1006557=note: print w/ replies, xml ) Need Help??


in reply to Illogical logic statement

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


Comment on Re: Illogical logic statement
Download Code
Re^2: Illogical logic statement
by fkento (Novice) on Dec 01, 2012 at 02:41 UTC
    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?

      eq

      Cheers Rolf

        Ah Hah!! Thank you! I forgot that you have to use 'eq' to compare strings. Wow I just wasted an hour trying to figure that one out. Thanks for the help.

      Try this:

      if ( 'f' eq 't' ) { print "f equals t\n"; } else { print "f does not equal t\n"; }

      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.

        Well naturally that should be formFileName.value +++ 1

        :p

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006557]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2014-09-22 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls