cmp operator in Perl is a string equality comparison operator used to compare if the two strings placed left and right to this operator are equal or less than the other.
Syntax: string1 cmp string2
Returns: -1 if string1 is less, 0 if equal and 1 if greater than string2.
Example 1: When String1 is less than String2
$a = "Geeks" ;
$b = "Welcome" ;
$c = $a cmp $b ;
print ( "Comparison of \$a and \$b returns $c" );
|
Output:
Comparison of $a and $b returns -1
Example 2: When String1 is equal to String2
$a = "Welcome" ;
$b = "Welcome" ;
$c = $a cmp $b ;
print ( "Comparison of \$a and \$b returns $c" );
|
Output:
Comparison of $a and $b returns 0
Example 3: When String1 is greater than String2
$a = "Welcome" ;
$b = "Geeks" ;
$c = $a cmp $b ;
print ( "Comparison of \$a and \$b returns $c" );
|
Output:
Comparison of $a and $b returns 1