Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can we mix "== "and "eq" in a single if statement ?

Status
Not open for further replies.

Rajey

Programmer
Feb 2, 2005
11
US
I do not know if perl will allow to compare number and sting variable in a single if statment

for e.g.

if (($current_transaction_key_code == $previous_transaction_key_code) && ($current_month_end_date eq $previous_month_end_date) && ($current_channel eq $previous_channel) )

here $current_transaction_key_code storsand $previous_transaction_key_code is a number


if it not possible do I need to convert everything in string and change the == to eq

I do not know how can we convert number to string also

kindly help out

Thanks


 
yes, it's perfectly acceptable. You can put as many as you want of any valid syntax.:

Code:
if ($n eq $r and $w > $t and $d != $b and $q lt $c or $q gt $u) {
   print "Thank God it passed all those comparisons!";
}
 
A number in perl is a string
So if you have
Code:
$foo = 3;

you can say 

If ($foo eq '3')

or you can say

If ($foo == 3)

so in the same way as Kevin said you can have

if ($foo eq '3' && ($foo2 == 4 or $foo3 eq 'whatever') || $foo5 gt 5 and $foo6 < 5) {
   print "I don't even know how this works.....kung fu\n";
}


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I don't know about this one:
Code:
$foo5 gt 5
'gt' is for strings so I would think you can't have a bareword after a string comparison operator, but maybe perl silently handles that problem. It might generate a warning though. I'll try it later when I have a chance and see what happens. Besides that though, numbers are strings in perl and perl usually can figure out the context of a number/string and do what is right.
 
'gt' is for strings so I would think you can't have a bareword after a string comparison operator
I'm a little disappointed Kevin.....but if you give it a try then i will forgive you. ;)


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
well, I tried it and there are no errors or warnings but the results of comparing numbers with 'gt' isn't what a person might expect since it's doing an ASCII sort (I believe). Like 5 is greater than 101 using 'gt' instead of '=='.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top