Home arrow Perl Programming arrow Page 5 - Perl 101 (Part 2) - Of Variables And Operators

Comparing Apples And Oranges - Perl

Now that you've got the basics of the language down, this secondarticle in the series teaches you about Perl's variables and operators, andalso introduces you to conditional expressions.

TABLE OF CONTENTS:
  1. Perl 101 (Part 2) - Of Variables And Operators
  2. Q
  3. 2 2 ...
  4. ... Or Two Plus Two
  5. Comparing Apples And Oranges
  6. Decisions! Decisions!
  7. Handling The Gray Areas
  8. Miscellaneous Notes
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
June 01, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
In addition to the various arithmetic and string operators, Perl also comes with a bunch of comparison operators, whose sole raison d'etre is to evaluate expressions and determine if they are true or false. Here's a list - you should use these operators for numeric comparisons only.

Assume $x=4 and $y=10

OperatorWhat It MeansExpressionResult
==is equal to$x == $yFalse
!=is not equal to$x != $yTrue
>is greater than$x > $yFalse
<is less than$x < $yTrue
>=is greater than or equal to$x >= $yFalse
<=is less than or equal to$x <= $yTrue


If, however, you're planning to compare string values, the two most commonly used operators are the equality and inequality operators, as listed below.

Assume $x="abc", $y="xyz"

OperatorWhat It MeansExpressionResult
eqis equal to$x eq $yFalse
neis not equal to$x ne $yTrue


You can also the greater- and less-than operators for string comparison - however, keep in mind that Perl uses the ASCII values of the strings to be compared when deciding which one is greater.

Assume $x="m", $y="M"

OperatorWhat It MeansExpressionResult
gtis greater than$x gt $yTrue
ltis less than$x lt $yFalse
geis greater than or equal to$x ge $yTrue
leis less than or equal to$x le $yFalse


The reason for this - the ASCII value of "m" is greater than the ASCII value of "M".

This article copyright Melonfire 2000. All rights reserved.

 
 
>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: