Hello Perl Monks, I am trying to understand the mechanics of string comparison in perl. The examples basically all show the same as this example I found online:

cmp Compare:
'a' cmp 'b' # -1
'b' cmp 'a' # 1
'a' cmp 'a' # 0

eq Equal to:
'a' eq 'b' # 0
'b' eq 'a' # 0
'a' eq 'a' # 1

ne Not-Equal to:

'a' ne 'b' # 1
'b' ne 'a' # 1
'a' ne 'a' # 0

lt Less than:
'a' lt 'b' # 1
'b' lt 'a' # 0
'a' lt 'a' # 0

le Less than or equal to:
'a' le 'b' # 1
'b' le 'a' # 0
'a' le 'a' # 1

gt Greater than:
'a' gt 'b' # 0
'b' gt 'a' # 1
'a' gt 'a' # 0

ge Greater than or equal to:
'a' ge 'b' # 0
'b' ge 'a' # 1
'a' ge 'a' # 1

So I took this thing and enclosed it in print "\n";. The code now looks like this:

sub main() { print "cmp Compare:\n"; print 'a' cmp 'b'."\n"; # -1 print 'b' cmp 'a'."\n"; # 1 print 'a' cmp 'a'."\n"; # 0 print "\n"; print "eq Equal to:\n"; print 'a' eq 'b'."\n"; # 0 print 'b' eq 'a'."\n"; # 0 print 'a' eq 'a'."\n"; # 1 print "\n"; print "ne Not-Equal to\n"; print 'a' ne 'b'."\n"; # 1 print 'b' ne 'a'."\n"; # 1 print 'a' ne 'a'."\n"; # 0 print "\n"; print "lt Less than\n"; print 'a' lt 'b'."\n"; # 1 print 'b' lt 'a'."\n"; # 0 print 'a' lt 'a'."\n"; # 0 print "\n"; print "le Less than or equal to\n"; print 'a' le 'b'."\n"; # 1 print 'b' le 'a'."\n"; # 0 print 'a' le 'a'."\n"; # 1 print "\n"; print "gt Greater than\n"; print 'a' gt 'b'."\n"; # 0 print 'b' gt 'a'."\n"; # 1 print 'a' gt 'a'."\n"; # 0 print "\n"; print "ge Greater than or equal to\n"; print 'a' ge 'b'."\n"; # 0 print 'b' ge 'a'."\n"; # 1 print 'a' ge 'a'."\n"; # 1 print "\n"; }
The desired results are already given in the example. In contrast, when I run the program, my shell tells me the following:
D:\perl>perl StringCompare.pl cmp Compare: -11-1 eq Equal to: ne Not-Equal to 111 lt Less than 11 le Less than or equal to 11 gt Greater than 1 ge Greater than or equal to 1
Now I'm all flabbergasted. What's going on there? As it doesn't print the '0's, it seems the only thing that gives the right number of '1's is "Less than or equal to". Everything else seems to do something unexpected. What did I do wrong here?


In reply to Newbie flabbergasted by string compare results by Anonymous Monk

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.