Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

Can someone explain this to a Monk Of Very Little Brain?

It caused me a great deal of confusion today.

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

Even stranger, the effect doesn't happen with smaller numbers:

if (111 > 0222){ print "yes\n" } else { print "no\n" } # prints "no";


“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

Replies are listed 'Best First'.
Re: When is 111111 greater than 0222222?
by The Mad Hatter (Priest) on Jun 30, 2003 at 05:29 UTC
    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:

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

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