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:
The desired results are already given in the example. In contrast, when I run the program, my shell tells me the following: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"; }
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?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
In reply to Newbie flabbergasted by string compare results by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |