Updated April 17, 2023
Introduction to Perl compare strings
The Perl compare strings is an essential operation for comparison between two string variables and their values. It is useful methods and operators to determine the equality or differentiation between two string values in the Perl Technology. It examine either two string values are equal or not equal using “eq” or “ne” operators in the Perl language. It also check one string value is greater than or less than another string value using “lt, gt, ge, le” comparator operators.
Syntax
The strings have to initialize for compare and other operations. The initialize string syntax is below.
$m1 = "welcome";
$n1 = "Perl"
The two strings are “equal to” operation syntax is below.
If($m1 eq $n1){ required coding … }
The two strings are “not equal to” operation syntax is below.
If($m1 ne $n1){ required coding … }
The two strings are “greater than” operation syntax is below.
If($m1 gt $n1){ required coding … }
The two strings are “less than” operation syntax is below.
If($m1 lt $n1){ required coding … }
The two strings are “greater than equal to” operation syntax is below.
If($m1 ge $n1){ required coding … }
The two strings are “less than equal to” operation syntax is below.
If($m1 le $n1){ required coding … }
The string is comparing with numeric value then used symbol for operation.
The “equal to” operation syntax is below.
If($m1 == 0){ required coding … }
How to compare strings in Perl?
- Download and install the Perl latest version in your operating system of the device.
https://www.perl.org/ or http://strawberryperl.com/ are mostly using the Perl IDE website link.
- Create a file with the .pl extension and save the file in the required command line path.
Example:
helloo.pl or firstpearl.pl
- Create Two String variables and initialize with the required value.
- Write dollar sign before variable name to understand string variable.
$m1 = “welcome”;
$b1 = “Perl”;
- Write down Perl compare Strings syntax below.
- Two strings are equal syntax is below.
if($m1 eq $b1)
{
print " string m1 is equal to String b1";
}
else
{
print "string m1 is not equal to String b1";
}
- Two strings are “not eR4qual to” the syntax is below.
if($m1 ne $b1)
{
print " string m1 is not equal to String b1";
}
else
{
print "string m1 is equal to String b1";
}
- All steps combine to get Perl to compare String example.
File name: myperl.pl
$a1 = "String";
$b1 = "String";
$c1 = "compare";
if($a eq $b)
{
print " string a1 is equal to String b1 \n";
}
else
{
print "string a1 is not equal to String b1 \n";
}
if($a1 ne $c1)
{
print " string a1 is not equal to String c1 \n";
}
else
{
print "string a1 is equal to String c1 \n";
}
- The dollar sign ( $a1 ) is utilizing for creating a variable in the Perl file.
- The Perl string value creates with double quotes ( “ Perl “ ) sign or single quotes (‘Perl ’)sign.
- The print is utilizing to display the output in the command line.
- Go to the command line window and write Perl with the filename (Perl myperl.pl).
- Press the “enter button” then you can see the required output on the window.
Examples of Perl compare strings
Different examples are mentioned below:
Example #1
The compare two strings with equal and not equal operator example and output.
Code:
$m1 = "String";
$b1 = "String";
$c1 = "compare";
if($m1 eq $b1)
{
print " string m1 is equal to String b1 \n";
}
else
{
print "string m1 is not equal to String b1 \n";
}
if($m1 ne $c1)
{
print " string m1 is not equal to String c1 \n";
}
else
{
print "string m1 is equal to String c1 \n";
}
Output:
Explanation:
- The eq operator is useful for the “equal to” operation in the Perl compare strings.
- The “ne” is useful for the “not equal to” operation in the Perl compare strings.
Example #2
The compare two strings using greater than and less than operator example and output.
Code:
$m1 = "String";
$c1 = "compare";
if($m1 eq $c1)
{
print " string m1 is equal to String c1 \n";
}
if($m1 gt $c1)
{
print " string m1 is greater than to String c1 \n";
}
if($m1 lt $c1)
{
print "string m1 is less than to String c1 \n";
}
Output:
Explanation:
- The “gt” is useful for “greater than” string operation in the Perl compare strings.
- The “lt” is useful for “less than” string operation.
Example #3
The “cmp” operator used for compare two strings example and output.
Code:
$d1 = "String";
$c1 = "compare";
$m1 = $d cmp $c;
$n1 = $c cmp $d;
print " String d1 compare with string c1 and returns are here: $m1; \n";
print "String c1 compare with string d1 and returns are here: $n1; \n";
Output:
Explanation:
- The “cmp” operator shows 1 when the first string greater than the second string.
- The “cmp” operator shows -1 when the first string less than the second string.
- The “cmp” operator displays 0 when the first string equal to the second string.
Example #4
The “== “operator used for compare two strings example and output.
Code:
$m = "String";
$c = "compare";
if($m == $c)
{
print "String m is equal to String b \n";
}
else
{
print "String m is equal to String b \n";
}
Output:
Explanation:
- The “==” operator compares the string object and memory location are the same or not.
Example #5
The strings comparison with case sensitive example and output.
Code:
$m = "String";
$b = "String";
$c = "string";
if($m eq $b)
{
print " string m is equal to String b \n";
}
else
{
print "string m is not equal to String b \n";
}
if($m ne $c)
{
print " string m is not equal to String c \n";
}
else
{
print "string m is equal to String c \n";
}
Output:
Explanation:
- The uppercase “S” used in the $m, $b variable, and lower case “s” is used in the $c variable.
Conclusion
The Perl compare string is useful for authentication and matching the two string variable and their value. It is useful for sorting, searching, and filtering more than one string value.
Recommended Articles
This is a guide to Perl compare strings. Here we discuss the introduction, how to compare strings in Perl? and examples respectively. You may also have a look at the following articles to lean more –