how does perl treat read-in numbers?

E

ela

When i read files of numbers delimited by tab, i discover an illogical if
condition of (1 > 37031) is fulfilled. i can only doubt that perl
misinterprets something as string instead of number. How to check whether it
treats it as a number or a string?
 
S

smallpond

When i read files of numbers delimited by tab, i discover an illogical if
condition of (1 > 37031) is fulfilled. i can only doubt that perl
misinterprets something as string instead of number. How to check whether it
treats it as a number or a string?

In a numeric expression perl treats a scalar as a number. Your
doubt is misplaced.

$n = "\t15\t";
print 1+$n;

16
 
T

Tad J McClellan

ela said:
Subject: how does perl treat read-in numbers?


All "read-in" data are strings.

When i read files of numbers delimited by tab, i discover an illogical if
condition of (1 > 37031) is fulfilled.


If you post a short and complete program that we can run, then we can
solve your problem. If you don't, we can't.

Have you seen the Posting Guidelines that are posted here frequently?

i can only doubt that perl
misinterprets something as string instead of number.


That cannot be it, because the condition is false even if both operands
are strings, because "1" is not greater than "3".

None of the statements below make any output...

print "true\n" if 1 > 37031;
print "true\n" if '1' > '37031';
print "true\n" if 1 gt 37031;
print "true\n" if '1' gt '37031';

How to check whether it
treats it as a number or a string?


By examining the operator that is being used.

">" treats its operands as numbers.
"gt" treats its operands as strings.
 
J

Jürgen Exner

ela said:
When i read files of numbers delimited by tab, i discover an illogical if
condition of (1 > 37031) is fulfilled. i can only doubt that perl
misinterprets something as string instead of number. How to check whether it
treats it as a number or a string?

No need to check anything, if a scalar is used in textual context then
it is treated as text, if it is used in numerical context then it is
treated as number (and as boolean if used in boolean context).

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,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top