Solved

Perl Strings

Posted on 1998-08-06
14
228 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:rrivers
  • 7
  • 5
  • 2
14 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1209059
In reverse order (and again, remember that there are at least 15 ways to do anything:)

$^w = 0;
$a += 0;
$^w = 1;

$a is now strictly numeric.

How does PERL determine if the variable is a string or is numeric?
To paraphrase Tom Christenson, it just does.  Don't worry about it.

If your variables are reasonable (relatively fixed format floats, or integers, or strings)
just use a string comparison if you really don't know whether, say, $a and $b are
strings or numbers.  You could also do a pattern match (m/) if you wanted, which,
intelligently done, could remove the difficulty in comparing floats.

By the way, that $^w (that's a ctrl-w, not carat and w) can be used to turn off the warnings about variables not numeric being treated as numeric.  Use carefully.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1209060
#that's $^W (and I'd prefer to localize it...)
{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.
0
 

Author Comment

by:rrivers
ID: 1209061
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.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1209062
I actually wonder about your assignment to $search. Try adding

print "\$search is actually \"$search\"\n";

Just before the line

if ($search eq "") {
0
 

Author Comment

by:rrivers
ID: 1209063
$search is exactly what I assign to it, in this case "cats" or "1-1-99" no /n or extra characters involved.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1209064
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?
0
 

Author Comment

by:rrivers
ID: 1209065
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.
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:b2pi
ID: 1209066
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"?  


0
 

Author Comment

by:rrivers
ID: 1209067
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.
 
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1209068
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"?  


0
 

Author Comment

by:rrivers
ID: 1209069
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.
 
0
 

Author Comment

by:rrivers
ID: 1209070
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.
 
0
 

Author Comment

by:rrivers
ID: 1209071
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.
 
0
 
LVL 84

Expert Comment

by:ozo
ID: 1209072
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add additional column to .csv using Perl. 8 128
Perl regex to replace any capital letters not preceded by ">" 6 108
quoting a comma separated list 20 81
Perl Frameworks 1 78
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now