Home arrow Perl Programming arrow Scalars and Variables

Scalars and Variables

In this fourth part of a five-part series on scalars in Perl, you learn how to compare the value of strings; we'll also wrap up our discusssion of operators and move on to variables. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars and Variables
  2. Operators to Be Seen Later
  3. Variables
  4. Operating and Assigning at Once
By: Apress Publishing
Rating: starstarstarstarstar / 1
April 15, 2010

print this article



String Comparison

As well as comparing the value of numbers, we can compare the value of strings. This does not mean we convert a string to a number, although if you say something like"12" > "30", Perl will convert to numbers for you. This means we can compare the strings alphabetically: “Bravo” comes after “Alpha” but before “Charlie”, for instance.

In fact, it’s more than alphabetical order; the computer is using either ASCII or Unicode internally to represent the string, and so has converted it to a series of numbers in the relevant sequence. This means, for example, “Fowl” comes before “fish”, because a capital “F” has a smaller ASCII value (70) than a lowercase “f” (102).1

1. This is not strictly true, though. Locales can define nonnumeric sorting orders for ASCII or Unicode characters that Perl will respect.

We can find a character’s value by using theord()function, which tells us where in the (ASCII) order it comes. Let’s see which comes first, a#or a*?

#!/usr/bin/perl -w
# ascii.pl

print "A # has ASCII value ", ord("#"), "\n";
print "A * has ASCII value ", ord("*"), "\n";

This should say

$ perl ascii.pl
A # has ASCII value 35
A * has ASCII value 42

If we’re only concerned with a character at a time, we can compare the return values oford()using the
<and>operators. However, when comparing entire strings, it may get a bit tedious. If the first character of each string is the same, you would move on to the next character in each string, and then the next, and so on.

Instead, there are string comparison operators that do this for us. Whereas the comparison operators for numbers are mathematical symbols, the operators for strings are abbreviations. To test whether one string is less than another, uselt. “Greater than” becomesgt, “equal to” becomeseq, and “not equal to” becomesne. There’s alsogeandlefor “greater than or equal to” and “less than and equal to.” The three-way-comparison becomescmp.

Here are a few examples of these:

#!/usr/bin/perl -w
# strcomp1.pl

print "Which came first, the chicken or the egg? ";
print "chicken" cmp "egg", "\n";
print "Are dogs greater than cats? ";
print "dog" gt "cat", "\n";
print "Is ^ less than + ? ";
print "^" lt "+", "\n";

And the results:

$ perl strcomp1.pl
Which came first, the chicken or the egg? -1 Are dogs greater than cats? 1
Is ^ less than + ?

The last line prints nothing as a result of"^" lt "+"since this operation returns the empty string indicating false.

Be careful when comparing strings with numeric comparison operators (or numeric values with string comparison operators):

#!/usr/bin/perl -w
# strcomp2.pl

print "Test one: ", "four" eq "six", "\n"; print "Test two: ", "four" == "six", "\n";

This code produces

$ perl strcomp2.pl
Argument "six" isn't numeric in numeric eq (==) at strcmp2.pl line 5.
Argument "four" isn't numeric in numeric eq (==) at strcmp2.pl line 5.
Test one:
Test two: 1

Is the second line really claiming that"four"is equal to"six"? Yes, when treated as numbers. If you compare them as numbers, they get converted to numbers."four"converts to 0,"six"converts to 0, and the 0s are equal, so our test returns true and we get a couple of warnings telling us that they were not numbers to begin with. The moral of this story is, compare strings with string comparison operators and compare numbers with numeric comparison operators. Otherwise, your results may not be what you anticipate.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: