Zero equals pipe??

I

ilya2

My perl program had some conditional statements that checked whether a
given characters was a pipe "|" or not. The program behaved strangely,
and I quickly realized it was treating pipes and zeros equivalently. To
make sure I was not going insane, I put the following checks:

if ( '1' == '|') { ... some statement ... }

if ( '0' == '|') { ... another statement ... }

By any sensible measure, both statements must return FALSE. First one
did that. Second one returned TRUE.

What's going on and how do I get around it?
 
K

kens

My perl program had some conditional statements that checked whether a
given characters was a pipe "|" or not. The program behaved strangely,
and I quickly realized it was treating pipes and zeros equivalently. To
make sure I was not going insane, I put the following checks:

if ( '1' == '|') { ... some statement ... }

if ( '0' == '|') { ... another statement ... }

By any sensible measure, both statements must return FALSE. First one
did that. Second one returned TRUE.

What's going on and how do I get around it?

My guess is that you do not want to do a numerical comparison.
Use 'eq' for a stringwise comparison.
Ken
 
J

John Bokma

My perl program had some conditional statements that checked whether a
given characters was a pipe "|" or not. The program behaved strangely,
and I quickly realized it was treating pipes and zeros equivalently. To
make sure I was not going insane, I put the following checks:

if ( '1' == '|') { ... some statement ... }

if ( '0' == '|') { ... another statement ... }

By any sensible measure, both statements must return FALSE.


== is for numbers, eq is for strings, which makes sense.

What makes even more sense, if you're a newbie to Perl, put on top of your
script:

use strict;
use warnings;

Since then you would have seen:

Argument "|" isn't numeric in numeric eq (==)
 
S

Sisyphus

..
..
if ( '0' == '|') { ... another statement ... }

Any string that begins with numeric characters will be assigned the numeric
value of those numeric characters. Hence:

'1' == 1
'0' == 0
'1234' == 1234
'1234garbage' == 1234
'12garbage34' == 12

But any string that begins with a non-numeric character will be assigned a
numeric value of 0.Hence:

'garbage1234' == 0
'|' == 0

From that you can see that '0' == '|' will evaluate as true since 0 == 0.

(This is documented somewhere .... but damned if I could find it in a hurry
:)

Cheers,
Rob
 
J

Jürgen Exner

My perl program had some conditional statements that checked whether a
given characters was a pipe "|" or not. The program behaved strangely,
and I quickly realized it was treating pipes and zeros equivalently.
To make sure I was not going insane, I put the following checks:

if ( '1' == '|') { ... some statement ... }

if ( '0' == '|') { ... another statement ... }

By any sensible measure, both statements must return FALSE. First one
did that. Second one returned TRUE.

What's going on and how do I get around it?

Well, the numerical values of your three strings are
'1' ==> 1
'0' ==> 0
'|' ==> 0
In other words you got the following comparions (partially evaluated):
if ( 1 == 0 ) { ... some statement ... }
if ( 0 == 0 ) { ... another statement ... }

If you had used warnings then perl would have told you.

You may also want to check the FAQ
"What's wrong with always quoting "$vars"?"
There is absolutely no need to quote your numbers and in fact those
superflous quotes led you down the wrong path.

jue
 
B

Bob Walton

Sisyphus said:
.
.

Any string that begins with numeric characters will be assigned the numeric
value of those numeric characters. Hence:

'1' == 1
'0' == 0
'1234' == 1234
'1234garbage' == 1234
'12garbage34' == 12

But any string that begins with a non-numeric character will be assigned a
numeric value of 0.Hence:

'garbage1234' == 0
'|' == 0

From that you can see that '0' == '|' will evaluate as true since 0 == 0.

(This is documented somewhere .... but damned if I could find it in a hurry
:)

That would be in

perldoc perlop

in the "Equality Operators" section.

....
 
S

Sisyphus

Bob Walton said:
That would be in

perldoc perlop

in the "Equality Operators" section.

I was trying to track down the documentation that details the way in which
perl converts strings to numbers. The account I provided is probably not so
bad .... if you consider the first '.' found in any string to be a
"numerical character" :)

I've possibly made other oversights, too.

Cheers,
Rob
 

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

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top