how does perl treat read-in numbers?

Discussion in 'Perl Misc' started by ela, Dec 8, 2008.

  1. ela

    ela Guest

    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?
     
    ela, Dec 8, 2008
    #1
    1. Advertising

  2. ela

    smallpond Guest

    On Dec 8, 9:04 am, "ela" <> wrote:
    > 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
     
    smallpond, Dec 8, 2008
    #2
    1. Advertising

  3. ela <> wrote:

    > 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.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Dec 8, 2008
    #3
  4. "ela" <> wrote:
    >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
     
    Jürgen Exner, Dec 8, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aleko
    Replies:
    0
    Views:
    332
    aleko
    Feb 25, 2005
  2. farmer
    Replies:
    3
    Views:
    330
    Grant Merwitz
    Aug 19, 2005
  3. Randall Parker
    Replies:
    1
    Views:
    587
    Kevin Spencer
    Oct 28, 2005
  4. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=

    Urllib.encode() - How does it Treat a List?

    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Jul 28, 2006, in forum: Python
    Replies:
    0
    Views:
    304
    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
    Jul 28, 2006
  5. Replies:
    12
    Views:
    166
    Tad McClellan
    May 18, 2006
Loading...

Share This Page