About > Computing & Technology > Perl/PHP 
Search      Web Hosting 
 Hi, I'm Emmie Lewis, your Guide to Perl/PHP. I can help you find what you need to know about Perl/PHP.

Perl/PHP

with Emmie Lewis
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· First Time at this site? START HERE
· Create and Use PHP Arrays
· PHP Function Reference
· How To Install A Perl Script
· About Perl Online Class
  BUYER'S GUIDE

Download Perl
Perl Installation
Beginning Perl
Intermediate Perl
Advanced Perl
PHP Installation
Beginning PHP
Intermediate PHP
PHP Weblogs
RSS
MySQL
Email
MacPerl/CGI
Group Calendars
Password Scripts
Beginning SQL
Database Scripts
PHP Redirection
OOP
Uploading Files
PHP - Print Pages
Counter Scripts
Regex
Web Servers
Image Scripts
Perl PHP and XML
File Locking
SQL Servers
MacPerl Modules
Debugging Perl

Subject Library

All articles on this topic

 
Stay up-to-date!
Subscribe to our newsletter.
 
ResultsAbout
New from About.com!
Get Paid to Improve Search
Find Results Now:
Management
E-Mail
Javascript
Internet
Java
 
 
Advertising
> Free Credit Report
 
   
How to compare values in Perl and PHP
Comparing strings in Perl
 More of this Feature
• Part 1: Comparison operators
 
 Join the Discussion
C/C++ from Perl
NATXIE
 

To compare string values in Perl, you must use the string comparison operators: "eq, ne, lt, gt, le, ge". That way, Perl knows it's supposed to compare the strings, and will not attempt to convert them into numbers first. By the way, Perl uses ASCII or Unicode to compare string values.

Here is a table of Perl's string comparison operators with some examples to show you how they're used. For all cases, assume that $x = 'a'.

Comparison String Operators Example Result
Equal to eq $x eq 'a' true
Not equal to ne $x ne 'b' true
Less than lt $x lt 'b' true
Greater than gt $x gt 'b' false
Less than or equal to le $x le 'b' true
Greater than or equal to ge $x ge 'b' false

Comparison and interpolation

By itself, comparison really isn't that exciting. Been there, done that, big yawn. However, because Perl uses these operators to decide how to interpret the value in a scalar variable, you do need to know how that happens. Basically, if you use the numeric operators to compare two scalars, then both the values are treated as numbers and then compared. If you use the string operators, then they're treated as strings and compared. So you see, knowing how Perl interpolates values is critical to understanding what will happen in conditional tests.

Converting numbers to strings

This one's easy: just use quotes. Having said that, I now need to clarify that once you do you can't always get the correct numeric value back from the string.

Sometimes underscores are used to make numbers easier to read (you can't use commas because they're reserved for separating lists):

$x = 1_000_000;
However, when this is converted to a string, the underscore characters do not get converted back into numbers. In other words, if
$x = 1_000_000;
$y = 1_000_000;
then if($x == $y) will return true.

However, if

$x = 1_000_000;
$y = '1_000_000';
it will not. Perl thinks it is comparing 1_000_000 to 1. Why? Because any non-numeric characters following a number aren't converted. Nor does Perl convert binary, hexadecimal or octal numbers into their decimal counterparts. We'll discuss that in detail in the next section, but knowing this much will help you understand that if you use quotes around numbers with underscores (1_000), hexadecimal numbers (0xFF), octal numbers (037), or exponential numbers (3.55E300), then they won't behave as you expect if you use numeric comparison operators to compare them.

Converting strings to numbers

When Perl tries to figure out the numeric value of a string, things get a little more complicated. First, it only tries to find a number at the beginning of a string. If it doesn't find one, the whole string is considered to be zero. Therefore,

$x = "abc123";
will evaluate to zero in a numeric comparison.

Also, Perl will only look at the first number in the string, and stops after it finds it. That's why if

$x = "1_000";
it will be converted to a value of 1. Also, if
$x = "10 20 30 40 50";
it will convert to the value of 10.

The same principles apply if you use decimal points in the string, such as "9.0". The value will returned as 9. Now, if you're trying to compare the numeric values of 9 to 9.0, they are going to pass as being equal. But since the string values of "9" and "9.0" are not equal, I need to bring this to your attention. It's important not to make assumptions when you're comparing strings to strings and numbers to numbers. You have to know the rules, and pay attention to them.

As mentioned previously, Perl will not convert binary, hexadecimal, or octal numbers into decimals. If you need to convert those numbers, use the hex() or oct() functions.

So how does all of this work?

Well, if you want to compare string values, use the string operators. Use numeric operators if you want to compare the numeric values.

Using the point made in the previous section, if

$x = 9;
$y = 9.0;
then
if($x == $y)
{
  # Returns true, because it's a numeric comparison
  # Numerically, 9 and 9.0 are equivalent
}
but
if($x eq $y)
{
  # Returns false because "9" is not the same string as "9.0"
  # Remember that because you're using a string comparison
  # operator, Perl is going to do the conversion for you.
}
and
if($x = $y)
{
  # Returns true because there's only one '=' sign
  # This assigns the value of 9.0 to $x
  # (Bang head on desk several times...)
}

Summary

So you see, the only complicated thing about all of this is remembering to use the right set of operators to compare values. Understanding the rules of interpolation is the real key to knowing how strings and numbers get converted back and forth. If you understand that, then the rest is easy.



Print this article


Subscribe to the Perl/PHP Newsletter
Name
Email

Previous Features



Email this page!

 Sponsored Links

Computer Training for $5.99 - Windows, Word, Excel
Learn how to use your computer! Windows, Word, Excel, FrontPage, Photoshop, Access, VB, ASP, Web Design, PowerPoint and more! Complete video courses just $5.99.
http://www.599cd.com/    Sponsored Link

Free Double-Sized Hard Drive and CD-RW Upgrade
Now thru Oct. 5th, get a free hard drive upgrade, free CD-RW upgrade, and free shipping when you buy a new HP Compaq d220 desktop with Intel® Pentium® 4 processor 2.40 GHz.
http://www.smb.compaq.com/    Sponsored Link

Access Your PC from Anywhere $19.95/Month
GoToMyPC is web-based remote-control software. It is a very easy and secure way to access and control your PC from any web browser.
http://www.gotomypc.com/    Sponsored Link

Save 15% on Digital Photography Software
With the Paint Shop Family, students of all ages and skill levels can embark on a digital photography adventure that will last a lifetime.
http://www.jasc.com/    Sponsored Link

Compare Web Hosts
Compare Web hosts by price and other features and find a Web hosting plan that fits your needs.
http://www.webhostanalyst.com/    Sponsored Link

Buy a Link Now!

     


Explore More on the About Network!
Related Sites
Focus on Java
Focus on JavaScript
Focus on Linux
Internet Technology
Personal Web Pages
PDA Hooray
Get your life in order. Palmtops/PDAs Guide William Hungerford explains why you need a PDA.
Rhyme Time
Children's Books Guide Elizabeth Kennedy picks the best Dr. Seuss books to start your collection.
Mature Workers
Senior Living Guide Jantje Blokhuis-Mulder has info on mature workers and early retirement.
Search About
  

About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2003 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.