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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use 'eq' or '=='? 2

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
I have never had any problems in dealing with ‘eq’ or ‘==’ until today. Below is a piece of my codes:

Code:
#! /usr/bin/perl

use strict;
use warnings;

my $hardcodedKernelInDouble = "2.6.5.7-244";
my $hardcodedKernelInSingle = '2.6.5.7-244';
my $kernelRealtime = `uname -r`;
chomp($kernelRealtime);
$kernelRealtime =~ s/^\D+//g;
$kernelRealtime =~ s/\D+$//g;
print "Real Time Kernel: #$kernelRealtime#\n";
if($kernelRealtime eq $hardcodedKernelInDouble) {
  [COLOR=blue][b]print "String match double: #$kernelRealtime#\n";[/b][/color]
}
else {
  print "String Not match double. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInDouble = #$hardcodedKernelInDouble#\n";
}
if($kernelRealtime eq $hardcodedKernelInSingle) {
  [COLOR=blue][b]print "String match single: #$kernelRealtime#\n";[/b][/color]
}
else {
  print "String Not match single. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInSingle = #$hardcodedKernelInSingle#\n";
}
if($kernelRealtime == $hardcodedKernelInDouble) { [b]# line 25[/b]
  print "Numeric match double: #$kernelRealtime#\n";
}
else {
  print "Numeric Not match double. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInDouble = #$hardcodedKernelInDouble#\n";
}
if($kernelRealtime == $hardcodedKernelInSingle) { [b]# line 31[/b]
  print "Numeric match single: #$kernelRealtime#\n";
}
else {
  print "Numeric Not match single. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInSingle = #$hardcodedKernelInSingle#\n";
}

The following is the output of a test run:

Code:
% uname -r
2.6.5-7.244-default
% ./test.pl
Real Time Kernel: #2.6.5-7.244#
String Not match double. $kernelRealtime = #2.6.5-7.244#, $hardcodedKernelInDouble = #2.6.5.7-244#
String Not match single. $kernelRealtime = #2.6.5-7.244#, $hardcodedKernelInSingle = #2.6.5.7-244#
[COLOR=red]Argument "2.6.5.7-244" isn't numeric in numeric eq (==) at ./strEqual.pl line 25.
Argument "2.6.5-7.244" isn't numeric in numeric eq (==) at ./strEqual.pl line 25.[/color]
[b]Numeric match double: #2.6.5-7.244#[/b]
[COLOR=red]Argument "2.6.5.7-244" isn't numeric in numeric eq (==) at ./strEqual.pl line 31.[/color]
[b]Numeric match single: #2.6.5-7.244#[/b]

I certainly understand the error messages in red and do expect them shown up. The things I don’t understand are:

1) Why does the error about line 25 show up twice?
2) Why do both numeric matches still succeed?
3) I did expect the codes in blue should be executed. Why weren’t they?


Thank you very much for your help!!
 
1) Don't know

2) == matches are for *numbers only*, eq is for strings (but works with numbers too). Perl gave you a warning about this for using == on something that wasn't a number (that dash in the middle makes it a string, numbers should only have numbers and decimals and maybe a - at the beginning if it's a negative number). But it corrected it for you on-the-fly and it still matched.

3) Look:

Code:
#2.6.5-7.244#, $hardcodedKernelInDouble = #2.6.5.7-244#

#2.6.5[red][b]-[/b][/red]7[b][red].[/red][/b]244#
#2.6.5[red][b].[/b][/red]7[b][red]-[/red][/b]244#

It's a "dash" 7 versus a "dot" 7.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Hi

whn said:
I have never had any problems in dealing with 'eq' or '==' until today.
And still used [tt]use warnings[/tt] pragma ? Without that [tt]perl[/tt] forgives this mistake.
whn said:
1) Why does the error about line 25 show up twice?
Because there are two non-numeric values, so one warning for each :
Code:
[blue]master #[/blue] perl -w -e 'print "1a" == "2b" || "3c" == "4d"'
Argument "2b" isn't numeric in numeric eq (==) at -e line 1.
Argument "1a" isn't numeric in numeric eq (==) at -e line 1.
Argument "4d" isn't numeric in numeric eq (==) at -e line 1.
Argument "3c" isn't numeric in numeric eq (==) at -e line 1.
My question would be, why only one warning for line 31.
whn said:
2) Why do both numeric matches still succeed?
Because those are warnings, not errors. [tt]perl[/tt] is able to compare two strings even you used wrong operator. Using [tt]==[/tt] you asked to transform the values into numbers and compare them. The transformation to numbers failed but the comparison was still performed.
Code:
[blue]master #[/blue] perl -w -e 'print "equal\n" if "3" eq "   003   "'

[blue]master #[/blue] perl -w -e 'print "equal\n" if "3" == "   003   "'
equal

Feherke.
 
Try This:

#! /usr/bin/perl
use strict;
use warnings;

my $hardcodedKernelInDouble = "2.6.5.7-244";
my $hardcodedKernelInSingle = '2.6.5.7-244';
my $kernelRealtime = `uname -r`;
chomp($kernelRealtime);
$kernelRealtime =~ s/^\D+//g;
$kernelRealtime =~ s/\D+$//g;
print "Real Time Kernel: #$kernelRealtime#\n";
if( "$kernelRealtime" eq "$hardcodedKernelInDouble" ) {
print "String match double: #$kernelRealtime#\n";
}
else {
print "String Not match double. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInDouble = #$hardcodedKernelInDouble#\n";
}
if( "$kernelRealtime" eq "$hardcodedKernelInSingle" ) {
print "String match single: #$kernelRealtime#\n";
}
else {
print "String Not match single. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInSingle = #$hardcodedKernelInSingle#\n";
}
if( "$kernelRealtime" eq "$hardcodedKernelInDouble" ) { # line 25
print "Numeric match double: #$kernelRealtime#\n";
}
else {
print "Numeric Not match double. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInDouble = #$hardcodedKernelInDouble#\n";
}
if( "$kernelRealtime" eq "$hardcodedKernelInSingle" ) { # line 31
print "Numeric match single: #$kernelRealtime#\n";
}
else {
print "Numeric Not match single. \$kernelRealtime = #$kernelRealtime#, \$hardcodedKernelInSingle = #$hardcodedKernelInSingle#\n";
}
 
At first, thank you, Kersel, very much for finding the typo for me!! I guess I must be on drugs so that I could tell what I was doing (I am taking some cold medicine which makes my dizzy all day).

Secondly, thanks Ejaggers. What changes do those doubles you added make? Some kind of casting? I'd rather to see those warnings.

Thirdly, to feherke, I know why it gave wanrings. But since I had typoes in my hard coded variables $hardcodedKernelInDouble & $hardcodedKernelInSingle, the match should fail no matter what, if my understanding is right. That makes me even wonder more!

Again, thanks all of you for you help.
 
Why do both numeric matches still succeed?

They both succeed because when you compare strings using == perl converts them to a number, in this case zero. So when you compare two strings (even unequal strings) using == you end up with:

Code:
if (0 == 0)

which of course is always true.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Just to correct a bit your otherwise informative answer, Kevin :), a string as [tt]"2.6.5.7-244"[/tt] evaluates to 2.6 when converted to a number. Perl interprets the string from the first char on, and goes on until it finds something that cannot represent a number in any of the available formats (a second dot for example): there it discards the rest of the string and takes the value of what has been found so far.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
ahh, yes, my bad. Thanks for the correction.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Wow, thank you, Kevin and Prex!! I appreciate your help very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top