in reply to When is 111111 greater than 0222222?

When you prepend a number with 0, perl interprets it as an octal value. Octal 222222 is decimal 74898. Octal 0222 is decimal 146. The '0222222' is turned into a number (from a string) and so therefore is effectively the same as 222222.

So your tests could be rewritten as:

if (111111 > 222222){ print "yes\n" } else { print "no\n" } # prints 'no' if (111111 > 74898){ print "yes\n" } else { print "no\n" } # prints "yes"; if (111111 > '0222222'){ print "yes\n" } else { print "no\n" } # prints "no"; if (111 > 146){ print "yes\n" } else { print "no\n" } # prints "no";

NB: Thanks to the Programmer's Cheat Sheet for the conversions.

Replies are listed 'Best First'.
Re: Re: When is 111111 greater than 0222222?
by Cody Pendant (Prior) on Jun 30, 2003 at 05:36 UTC
    I knew there'd be an obvious explanation. Thank you so much. Where is that documented, by the way?

    Note for other monks, if you invent a file-naming system based on dates, use '2003' instead of '03' at the start and you won't have this problem. D'oh!



    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
        Thanks PodMaster.



        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D
      I'm not sure where it is formally documented, but it is in a perlfaq4 question: perldoc -q octal. The prepending of 0 comes from C (IIRC), but it might have originated earlier. BTW, 0x is used to specify hex values.

      Heh. You should always use full dates anyway. ; )

      It's documented in the perldata manual page, in the section Scalar value constructors.

      Abigail

      ...if you invent a file-naming system based on dates, use '2003' instead of '03' at the start...

      Or even, find out about ISO date formats, particularly the "Complete date" format!

      .02

      cLive ;-)

      There was a little thing a couple of years ago - you might have heard of it - called 'the millennium bug'.

      Jasper
        I should say, in my defense, that this code will never have to deal with dates before '01, and unless something truly bizarre happens, won't even have to deal with dates after '05. I won't have problems until 2010.

        I know that's exactly what millennium bug people thought, but seriously, an SQL database is coming long before it would be an issue, with proper dates. If all else fails I'll mass-rename and rewrite a couple of subs and all will be well again.

        Can I use this space to ask what happens to my unfortunately-constructed numbers in the following circumstances:

        1. $number = '022222'
        2. $number = "022222"
        3. $number = 022222
        4. @numbers = READDIR(something); $number = $numbers[0] # assuming my file's called '022222'
        ?

        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D