Lunarpages Web Hosting Forum

Author Topic: String Comparisons in PERL  (Read 2033 times)

Offline TWebMan

  • Quantum Encyclopedia Writer
  • *****
  • Posts: 3047
    • Take charge of your site
String Comparisons in PERL
« on: March 31, 2002, 10:11:00 AM »
When doing a string comparison in perl, you want to remember that perl treats numbers and text differently (unlike php, c, java, etc...).
The string comparisons are eq, ne, gt, lt, and a few others. (eq is equals, ne, not equals, gt is greater than, lt is less than).
HOWEVER - if you have a combination of numbers, and you need to use an if statement, the rules change!  For integer/numeric comparisons, you're probably safe using the normal =, <, >, ==, =>, =<.  But if you put quotes around numbers with non-numerical characters, perl will turn them into zeroes and can cause a problem.
In an if statement, the rules sorta change.  Without getting to technical, here's a tip.
If you need to compare a string in a variable,
make a new variable with the string.  For example, if $thisvar = "yup13" needs to set a condition, then make a variable with that string and compare variables. So,

$compareto = "yup13";
if ($thisvar = $compareto){do this;}

Instead of

if ($thisvar = "yup13"){do this;}

In fact, you're better setting up a condition if something DOES NOT equal something else.  Your results will probably more what you wanted.
"Computers cause people to make more mistakes than any other invention in history, with the possible exception of handguns and tequila."  - Unknown
"Liberty of any kind is seldom lost all at once." - D. Hume
Every day is an Ode to Joy
The planet will be fine... and so will your site