Comparison Operators in Perl

Now that we've studied the basics of Perl, let's learn more complex things that will make our scripts even more interesting and useful.

Let's learn how to compare numbers and strings in this Perl tutorial.

Numerical Comparison Operators in Perl

There are 6 comparison operators, which we can use with numbers, in Perl, and they are quite simple as they look like the ones we use in math.

The operators are:
  1. Equal: ==
  2. Not equal: !=
  3. Greater than: >
  4. Less than: <
  5. Greater than or equal to : >=
  6. Less than or equal to: <=
We use them like this:

  • value1 operator value2

And this operation always returns 1 (TRUE ) or empty (FALSE or 0).

For example, run the following script:
#!/usr/bin/perl

print "2==2: ",2==2,"\n";
He will return: 1
What print does is the result of operation 2 == 2

This operation is a comparison operation, you're asking: 2 equals 2?
He answers truthfully (represented by 1).

Now test this:
#!/usr/bin/perl

print "2==2: ",2==2,"\n";
print "1>2 : ",1>2,"\n";
The result is:
2 == 2: 1
1> 2:

When asked if 1 is greater than 2, it responds with empty, nothing, null ... which is the same as false in Perl.

What if we ask if 1 is different from 2?
We do this with: 1! = 2, and it's a true statement, test the script:
#!/usr/bin/perl

print "2==2: ",2==2,"\n";
print "1>2 : ",1>2,"\n";
print "1!=2: ",1!=2,"\n";
Everything worked?

The operation: $ a <$ b
Returns true (1) if $a is less than $b, and false if  a is greater than or even equal to $b.

Already the expression:
$a >= $b
Returns true (1) if $a is greater than or equal to $b and returns false (null) if $a is less than $b.

Finally, the operation:
$a <= $b
Returns 1 if $a is smaller or if $a is equal to $b. If $a is greater than $b it returns false (null).

Perl string comparisons

To compare strings, we will use some small letters, which represent the same thing as the operators above:

  1. Equal to: eq
  2. Not equal to: ne
  3. Greater than: gt
  4. Less than: lt
  5. Greater than or equal to: ge
  6. Less than or equal to: le

That is, the string comparison operators in Perl are:
eq (equal), ne (not equal), gt (greater than), lt (less than), ge (greater than or equal to) and le (less than or equal to).

The comparison is done character by character one by one, comparing to know if the strings are equal or not.

Let's look at some examples:
#!/usr/bin/perl

print "rush eq rush: ", 'rush' eq 'rush',"\n";
print "rush ne Rush: ",'rush' ne 'Rush',"\n";
print "abc gt ab   : ",'abc' gt 'ab',"\n";
print "bc lt bcd   : ",'bc' lt 'bcd',"\n";
Note that in the ASCII standard, uppercase letters come before lowercase:
#!/usr/bin/perl

print "A lt a: ", 'A' lt 'a',"\n";
print "b gt B: ",'B' ne 'b',"\n";
That is, 'A' is less than 'a'
And 'b' is bigger than 'B'

Test the scripts, guys. See the results.
All these operations are like 'questions' to Perl, which answers 1 or null, that is, true or false.

And be careful not to confuse:
$a == 1: is a comparison, is a question '$a equals 1?', generates true or false return

And:
$a = 1: it's an assignment, $a will get the value 1 and period, it's over there.

No comments:

Post a Comment