Comparison operators are used for decision making. They let you compare one thing to another.
Most of the above comparisons produce a true/false value except the last one. Here is a quick example to show how it works :
$result equals 1 if $a is greater than 5.
$result equals -1 if $a is less than 5.
$result equals 0 if $a is equal to 5.
Here is a larger example to show comparisons in action :
Strings (values containing letters and such) also have comparison operators, they just look a bit different than the number ones above. The operator takes the first character of each string and compares them.
Some sample results :
|
< > == <= >= != <=> |
less than greater than equal to less than OR equal to greater than OR equal to not equal to comparison |
Most of the above comparisons produce a true/false value except the last one. Here is a quick example to show how it works :
| $result = $a<=>5; |
$result equals 1 if $a is greater than 5.
$result equals -1 if $a is less than 5.
$result equals 0 if $a is equal to 5.
Here is a larger example to show comparisons in action :
|
#!/usr/bin/perl print "Content-type: text/html\n\n; $x=5; $y=15.5; $z=.05; $a=$x<6; print "a equals $a<br>"; $b=$x>6; print "b equals $b<br>"; $c=$x>=5; print "c equals $c<br>"; $d=$x>=5; print "d equals $d<br>"; $e=$x==5; print "e equals $e<br>"; $f=$x!=5; print "f equals $f<br>"; $g=$x<=>6; print "g equals $g"; |
Strings (values containing letters and such) also have comparison operators, they just look a bit different than the number ones above. The operator takes the first character of each string and compares them.
|
lt gt eq le ge ne cmp |
less than greater than equal to less than or equal to greater than or equal to not equal to comparison |
Some sample results :
|
"dog" lt "cat" "345" gt "62" "dog" le "cat" "dog" ge "cat" "dog" eq "cat" "dog" ne "cat" "dog" cmp "cat" |
false. false. false. true. false. true. 1 |
c is less than d. 3 is less than 6. c is still less than d. d is greater than c. The two are not equal. They are not equal. d is greater than c. |

