Perl

PERL String Comparison

“Many options exist in PERL to compare string values. One way is to use the “cmp” operator, and another way is to use comparison operators, which are “eq,” “ne,” “lt.” and “gt.” The “==” operator is used for number comparison only in PERL. Different ways of comparing two strings in PERL have been shown in this tutorial.”

Compare Strings Using the “cmp” Operator

The “cmp” operator is used for comparing two strings and finding out if the first is less than or equal to, or greater than the second string. Create a PERL file with the following code to know the uses of the “cmp” operator in PERL. Two string values will be taken from the user for the comparison. The chomp() function has been used in the code to remove the newline from both string values taken from the user. Next, the output of the “cmp” operator will be stored in a variable. The return value of the “cmp” operator will be 0 if both strings are equal. The return value of the “cmp” operator will be 1 if the first string is greater than the second string. The return value of the “cmp” operator will be -1 if the first string is less than the second string.

# Take two string values

print "Enter the first string: ";

$Str1 = <STDIN>;

print "Enter the second string: ";

$Str2 = <STDIN>;

 

# Remove newline from each string variable

chomp($Str1, $Str2);

 

# Compare the string variables using 'cmp' operator

$output = $Str1 cmp $Str2;

 

# Check the output of the 'cmp' operator

if ($output == 0)

{

    print "Both strings are equal.\n";

}

elsif($output  == 1)

{

    print "$Str1 is greater than $Str2\n";

}

else

{

    print "$Str1 is less than $Str2\n";

}

Output

According to the following output, the code has been executed three times. The first input value was “hello,” and the second input value was “Hello” in the first execution of the code. The character “h” is greater than the character “H” based on the ASCII value of these characters. So, “hello is greater than Hello” has been printed in the first output. For the same reason, “Hello is less than hello” has been in the second output. Both “hello” and “hello” are equal that have been taken in the third execution of the code. So, “Both strings are equal.” has been printed in the third output.

Compare Strings Using the “eq” Operator

The “eq” operator is used in PERL to find out whether two string values are equal or not. Create a PERL file with the following code to know the use of the “eq” operator for comparing string values. Two string values have been stored into two variables. The first word of both string values is equal, and that is “Good,” but the second part of the string values is not equal. So, the “eq” operator will return a false value.

# Declare two string variables

$Str1 = "Good Morning";

$Str2 = "Good Bye";

 

# Compare the strings using 'eq' operator

if($Str1 eq $Str2)

{

    print "Both strings are equal.\n";

}

else

{

    print "The strings are not equal.\n";

}

Output

The following output will appear after executing the above code.

Compare Strings Using the “ne” Operator

The “ne” operator is used in PERL to find out whether two string values are not equal or not. Create a PERL file with the following code to know the use of the “ne” operator for comparing string values. Two string values have been stored into two variables. The first 4 characters of both string values are equal, and that is “Good,” but the second part of the string values is not equal. Here, the substr() function has been used to cut the first 4 characters from both string values, and next, the values have been compared with the “ne” operator. So, the “ne” operator will return a false value.

# Declare two string variables

$Str1 = "Good Morning";

$Str2 = "Good Bye";

 

# Compare the particular part of the strings using 'ne' operator

if(substr($Str1,0,4) ne substr($Str2,0,4))

{

    print "The first 4 characters of both strings are not equal.\n";

}

else

{

    print "The first 4 characters of both strings are equal.\n";

}

Output

The following output will appear after executing the above code.

Compare Strings Using the “lt” Operator

The “lt” operator is used in PERL to find out whether the first string is less than the second string or not. Create a PERL file with the following code to know the use of the “lt” operator for comparing string values. Here, the substr() function has been used to cut the characters from position 5 to the remaining part of both strings. Next, the values have been compared with the “lt” operator. Here, the character “M” is greater than the character “B” based on the ASCII value. So, the “lt” operator will return a false value.

# Declare two string variables

$Str1 = "Good Morning";

$Str2 = "Good Bye";

 

# Compare the particular part of the strings using 'lt' operator

if(substr($Str1,5) lt substr($Str2,5))

{

    print "$Str1 is less than $Str2\n";

}

else

{

    print "$Str1 is greater than $Str2\n";

 

}

Output

The following output will appear after executing the above code.

Compare Strings Using the “gt” Operator

The “gt” operator is used in PERL to find out whether the first string is greater than the second string or not. Create a PERL file with the following code to know the use of the “gt” operator for comparing string values. Two string values will be taken from the user. The chomp() function has been used here to remove the newline from the input values before comparing the values using the “gt” operator.

# Take two string values

print "Enter the first string: ";

$Str1 = <>;

print "Enter the second string: ";

$Str2 = <>;

 

# Compare the strings using 'gt' operator

if(chomp($Str1) gt chomp($Str2))

{

    print "$Str1 is greater than $Str2\n";

}

else

{

    print "$Str1 is less than $Str2\n";

}

Output

The following output will appear after executing the above code for the input values, “Hello World” and “Hello world.”

Conclusion

Different ways of comparing string values in PERL have been shown in this tutorial by using multiple comparison operators. The PERL users can use any of the comparison operators to compare the string values in PERL.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.