2

What's the Perl function that achieves the same thing as compareTo() in Java? I know about eq and ne but I want to compare to see if one string is greater than another.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
vette982
  • 4,782
  • 8
  • 34
  • 41
  • 4
    You'll get better answers to questions like this if you explain exactly what `compareTo` does in Java. Not all Perl programmers know Java. – cjm Aug 23 '10 at 21:01

2 Answers2

17

You actually have cmp (for strings) and <=> (for numbers) operators. See the Equality Operators section in perlop.

Example:

print "foo" cmp "bar"; # prints 1
cjm
  • 61,471
  • 9
  • 126
  • 175
phadej
  • 11,947
  • 41
  • 78
5

gt should do the trick

Edit: actually, cmp would be more similar to compareTo(), gt would just tell you if the string is greater than then the other.

cam
  • 14,192
  • 1
  • 44
  • 29