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!

When to use eq and ==

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Hello all,

How can I tell for sure if the variable I am operating on is a string or an integer?

Can't 01 also be a string "01"??

Thanks

I got a Biz Degree! How the h*ll did I get here?
 
Perl can determine the type of a variable depending on the operator and context

01 is technically 1
"01" is a string, but can be 1 in a numerical context

$var eq 1 is wrong
$var == 1 is right
$var eq "01" is right
$var == "01" is wrong

I hope I got that right
--Paul

cigless ...
 
Ok Paul...how about this one:

Code:
my $date = "03-20-2005";

my ($month,$day,$year) = split (/-/, $date);

Is $month an integer or string?



I got a Biz Degree! How the h*ll did I get here?
 
String to my knowledge. In fact, a scalar. If you say

Code:
if ($month == 3)

it will accept and is true as internal conversion will be done.
 
if all the characters in the string are numeric use ==, else use eq.
 
A little confusion here I see. Paul, you say both, does that mena either eq or == will work?



I got a Biz Degree! How the h*ll did I get here?
 
Paul's quite right, if you have a string containing '01' it can be interpreted by Perl in a couple of ways.

As a string '01', so the test $var eq '01' will evaulate true.

As a number 01 or 1, so the test $var == 1 will evaluate true.

What won't work is this $var eq '1' or $var eq 1. Both of these compare $var to the string '1', which it won't match.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top