Need help with relational operators in PERL!!

Status
Not open for further replies.

Sumitha Sudhakaran

Junior Member level 2
Joined
Aug 7, 2012
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangalore
Activity points
1,440
I want to compare 2 multi digit numbers, say $x = 99 and $y = 158.. the operation: if ($x gt $y)
{print $x;}

the result gives 99 as the answer.. Probably because 9 is greater than 1.. Wats the command to get the correct answer in PERL???
 

The problem is you are using the wrong relational operator. "gt" is for comparing two strings. For example,

Code:
$string1 = 'cat';
$string2 = 'dog';

    if ($string1 ne $string2) {
        print "Not equal\n";
    } else {
        print "Equal\n";
    }

You need to use < or > for integers.
Code:
$x = 99;
$y = 158;

if ($x > $y){
print $x;
} else {
print "Not greater than";
}

Hope this helps
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…