Broad Network


Perl Rational and Equality Operators

Perl Operators – Part 3

Perl Course

Foreword: In this part of the series, I talk about equality and relational operators in Perl.

By: Chrysanthus Date Published: 12 Oct 2015

Introduction

This is part 3 of my series, Perl Operators. In this part of the series, I talk about equality and relational operators in Perl. Equality operators are the numeric ==, != and <=> operators, and the stringwise eq, ne, and cmp operators (see explanation below). Relational operators are the numeric <, >, <= and >= operators and stringwise lt, gt, le and ge operators (see explanation below). You should have read the previous parts of the series before reaching here, as this is a continuation.

Equality Operators
There are two categories of Equality operators: numeric equality operators and string equality operators. Numeric equality operators are the == and != operators explained as follows:

The Numeric Equal Operator
It is ==, typed as a double assignment operator. The equal operator returns true if the arguments on either side are numerically equal, otherwise it returns false. Never use the assignment operator in place of the equality operator.

The Numeric Not Equal Operator
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the arguments are not equal, otherwise it returns false. Let us look at some examples:

Try the following code:

use strict;

    my $myIdent = 25;
    my $hisIdent = 30;
    if ($myIdent != $hisIdent)
        {
            print "The numbers of the two variables are not equal.";
        }

$myIdent is 25, $hisIdent is 30. The condition is read like this: If $myIdent is not equal to $hisIdent, (which is true in this case) then the if-block will be executed. Since the values of the variables are not equal, ($myIdent != $myIdent) returns true; the if-block is executed.

In the following code, the values of the two variables are equal, so the condition returns false and the if-block is not executed.

use strict;

    my $myIdent = 50;
    my $hisIdent = 50;
    if ($myIdent != $hisIdent)
        {
            print "The numbers of the two variables are not equal.";
        }

The <=> Operator
This operator returns –1 if the left argument is less than the right argument. It returns 0 if the two arguments are equal. It returns 1 if the left argument is greater then the right argument. Try the following code:

use strict;

    print 5 <=> 8, "\n";
    print 6 <=> 6, "\n";
    print 7 <=> 4, "\n";

The output is:

-1
0
1

To use this in a condition, remember that –1 and 1 are equivalent to true, while 0 is equivalent to false.

Now, the string equality operators are eq and ne as explained below:

The String eq Operator
Here, eq means, equal to. If two strings have the same characters in the same order, then the two strings are equal to one another. The casings (upper and lower) of letters also have to be the same. Read and try the following program:

use strict;

    my $myIdent = "xyz abc";
    my $hisIdent = "xyz abc";
    if ($myIdent eq $hisIdent)
        {
            print "The strings of the two variables are equal.";
        }

The two strings have the same characters in the same order.

The String ne Operator
Here, ne means, not equal to. This operator is the opposite of the previous. Read and try the following program:

use strict;

    my $myIdent = "xyz abc";
    my $hisIdent = "xyzabc";
    if ($myIdent ne $hisIdent)
        {
            print "The strings of the two variables are not equal.";
        }

In this case the strings are not equal because one has the space character and the other does not.

Note: same characters with same casing and same placements will always result in equal strings. If any of these three parameters is changed, the strings become unequal.

The cmp Operator
This operator returns –1 if the left argument is stringwise less than the right argument. It returns 0 if the two arguments are stringwise equal. It returns 1 if the left argument is stringwise greater then the right argument. Read and try the following code:

use strict;

    print "alphabetical" cmp "order", "\n";
    print "bcde"<=> "bcde", "\n";
    print "man" cmp "MAN", "\n";

The output is:

-1
0
1

To use this in a condition, remember that –1 and 1 are equivalent to true, while 0 is equivalent to false.

Relational Operators
<, >, <= and >= are numerical relational operators and lt, gt, le and ge are string relational operators.

The Greater Than Numerical Operator
The Greater Than Numerical operator is, > . It returns true if the left argument is greater than the right argument. In the following example, the left argument is greater than the right argument. So the if-block is executed:

use strict;

    my $ident1 = 60;
    my $ident2 = 70;
    if ($ident2 ne $ident1)
        {
            print 'The value of $ident2 is greater than the value of $ident1.';
        }

Try the this code.

Greater Than Or Equal Numerical Operator
The Greater Than or Equal Numerical operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left argument is greater than or equal to the right argument.

The Less Than Numerical Operator
The Less Than Numerical Operator is < .It returns true if the left argument is less than the right argument.

The Less Than or Equal Numerical Operator
The Less than or Equal Numerical operator is, <= . It returns true if the left argument is less than or equal to the right argument.

The gt String Operator
This is the Greater Than Operator for the string. It compares two strings alphabetically, the way they are placed in the dictionary. A string that would be typed last in the dictionary, is the greater string. That is, “bbb” is greater than “aaa”. Read and try the following program that illustrates this:

use strict;

    my $ident1 = "aaaa ccccccc";
    my $ident2 = "d";
    if ($ident2 gt $ident1)
        {
            print 'The value of $ident2 is greater than the value of $ident1.';
        }

The if-block is executed, because "d" is greater than "aaaa ccccccc".

In the Perl alphabetic series, uppercase letters come before lowercase letters. That is, the series, is A, B, C, …Z, a, b, c …z. Read and try the following code that illustrates this:

use strict;

    my $ident1 = "QQQQ";
    my $ident2 = "eeee";
    if ($ident2 gt $ident1)
        {
            print 'The value of $ident2 is greater than the value of $ident1.';
        }

The if-block is executed.

When numbers are considered as characters (in a string), they fit alphabetically in front of ‘A’. Here, the Perl numeric-alpha series is, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, …a, b, c,…. So “BB” is greater than “33”. Read and try the following program that illustrates this:

    my $ident1 = "22aa";
    my $ident2 = "aa22";
    if ($ident2 gt $ident1)
        {
            print 'The value of $ident2 is greater than the value of $ident1.';
        }

The if-block is executed.

The lt String Operator
This is the Less Than string operator. It behaves in the opposite way to gt.

The ge String Operator
This is the Greater Than or Equal To string operator. It returns true if the left argument is the same in content (characters and order) as the right argument. It also returns true if the left argument is greater than the right argument, just like the above code samples. Read and try the following program, where the two arguments are the same:

use strict;

    my $ident1 = "e e e";
    my $ident2 = "e e e";
    if ($ident2 ge $ident1)
        {
            print 'The value of $ident2 is greater than the value of $ident1.';
        }

The le String Operator
This is the Less Than or Equal To string operator. It does the opposite of, ge.

In Perl, the equality and relational operators are non-associative; this means operation from left to right or from right to left is not important, making it similar to mathematics.

Associativity
The equality and relational operators in Perl, are non-associative. This means that if you have consecutive such operators, other factors will come in, to decide which operator operates first.

That is it for equality and relational operators. Let us take a break here. Rendezvous in the next part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message