rrivers
asked on
Perl Strings
I am trying to write an if statement to compare 2 variables.
The problem I am running into is that there are 2 types of comparision tests in PERL, one is numerical (==, !=, <=), the other is string (eq, ne, le). The variables I am trying to compare can be either string or numerical. So the question is, How does PERL determine if the variable is a string or is numeric? And is there a way I can make all variables just string or just numeric?
The problem I am running into is that there are 2 types of comparision tests in PERL, one is numerical (==, !=, <=), the other is string (eq, ne, le). The variables I am trying to compare can be either string or numerical. So the question is, How does PERL determine if the variable is a string or is numeric? And is there a way I can make all variables just string or just numeric?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried to use just a string comparison, it doesn't work. I have the variable $search. That variable can either be the word cats or the date (such as 1-1-99).
I then have the loop that says
if ($search eq "")
{
Do something...
}
When $search become the date 1-1-99, the comparison doesn't work. However if $search becomes cats the comparison works.
But if I have :
if ($search == "")
{
Do something...
}
When $search become the date 1-1-99, the comparison works. However if $search becomes cats the comparison fails.
So simply just saying the item is a string does not work.
I then have the loop that says
if ($search eq "")
{
Do something...
}
When $search become the date 1-1-99, the comparison doesn't work. However if $search becomes cats the comparison works.
But if I have :
if ($search == "")
{
Do something...
}
When $search become the date 1-1-99, the comparison works. However if $search becomes cats the comparison fails.
So simply just saying the item is a string does not work.
I actually wonder about your assignment to $search. Try adding
print "\$search is actually \"$search\"\n";
Just before the line
if ($search eq "") {
print "\$search is actually \"$search\"\n";
Just before the line
if ($search eq "") {
ASKER
$search is exactly what I assign to it, in this case "cats" or "1-1-99" no /n or extra characters involved.
Did you actually put in the debug line? On my system:
% perl
$search = "1-1-99";
print "\$search is actually \"$search\"\n";
if ($search eq "") {
print "Huh? it's Blank!\n";
} else {
print "Nope, it isn't blank\n";
}
^D
$search is actually "1-1-99"
Nope, it isn't blank
%
Does your system have different results?
% perl
$search = "1-1-99";
print "\$search is actually \"$search\"\n";
if ($search eq "") {
print "Huh? it's Blank!\n";
} else {
print "Nope, it isn't blank\n";
}
^D
$search is actually "1-1-99"
Nope, it isn't blank
%
Does your system have different results?
ASKER
Yes, I did a debug. $search is blank because it contains the information from a field on a HTML form. So if there is nothing in the field $search is blank. I have tested this and if the text field is not filled in the variable remains empty.
OK, now I'm confused. You're saying that $search is blank, but
if ($search eq "") {
print "it's blank\n";
} else {
print "it's not blank\n";
}
By the way, both 'cats' and '1-1-99' are strings.
would print "it's not blank\n"?
if ($search eq "") {
print "it's blank\n";
} else {
print "it's not blank\n";
}
By the way, both 'cats' and '1-1-99' are strings.
would print "it's not blank\n"?
ASKER
Yes, I did a debug. $search is blank because it contains the information from a field on a HTML form. So if there is nothing in the field $search is blank. I have tested this and if the text field is not filled in the variable remains empty.
OK, now I'm confused. You're saying that $search is blank, but
if ($search eq "") {
print "it's blank\n";
} else {
print "it's not blank\n";
}
By the way, both 'cats' and '1-1-99' are strings.
would print "it's not blank\n"?
if ($search eq "") {
print "it's blank\n";
} else {
print "it's not blank\n";
}
By the way, both 'cats' and '1-1-99' are strings.
would print "it's not blank\n"?
ASKER
Yes, I did a debug. $search is blank because it contains the information from a field on a HTML form. So if there is nothing in the field $search is blank. I have tested this and if the text field is not filled in the variable remains empty.
ASKER
Yes, I did a debug. $search is blank because it contains the information from a field on a HTML form. So if there is nothing in the field $search is blank. I have tested this and if the text field is not filled in the variable remains empty.
ASKER
Yes, I did a debug. $search is blank because it contains the information from a field on a HTML form. So if there is nothing in the field $search is blank. I have tested this and if the text field is not filled in the variable remains empty.
Neither 'cats' nor '1-1-99' are eq ""
$search=="" is equivalent to $search==0
($search='cats') == 0 (which would give you the warning Argument "cats" isn't numeric if you set $^W)
is equivalent to 0==0
($search='1-1-99') == 0 (which would also give a -w warning)
is equivalent to 1==0
$search=="" is equivalent to $search==0
($search='cats') == 0 (which would give you the warning Argument "cats" isn't numeric if you set $^W)
is equivalent to 0==0
($search='1-1-99') == 0 (which would also give a -w warning)
is equivalent to 1==0
{local $^W=0; $a += 0;}
#how do YOU determine whether you consider your variable to be string or numerical?
#Perl should be following your intentions, you shouldn't have to be second guessing it.