Hi,

I hit some strange behavior when doing simple comparisons of numbers with leading zeros.

I found that if I don't quote a number I am using for comparison, I sometimes get incorrect result.

Here is my code that demonstrates the issue:

#!/usr/bin/perl -w use strict; my @numbers = qw(022000 022300 022400 022800 023600 024700 024500 0249 +00); foreach my $number(@numbers) { my $compRes1 = $number < 060000; my $compRes2 = $number < '060000'; print "For $number, result1 is $compRes1, result2 is $compRes2\n"; }

Here is the output that I get:
For 022000, result1 is 1, result2 is 1
For 022300, result1 is 1, result2 is 1
For 022400, result1 is 1, result2 is 1
For 022800, result1 is 1, result2 is 1
For 023600, result1 is 1, result2 is 1
For 024700, result1 is , result2 is 1
For 024500, result1 is 1, result2 is 1
For 024900, result1 is , result2 is 1

As can be seen, for '024700' and '024900', result1 is empty while result2 is correct.

I tried this both on 32 and 64 bit Perl, older and newer versions.

I am not sure if this is a problem, just very strange.

Any ideas as to how this can happen are greatly appreciated.


Tim.

In reply to strange comparison results by sduser81

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.