1. Technology

Comparing values in Perl

By

Man using laptop
ColorBlind/Photodisc/Getty Images

At some point very on in your programming career, you are going to want to make your program do different things depending on the status of a particular variable.

For example, your program may be counting the number of occurrences of a given string within an input file. When it is ready to display this to the user, it needs to display a grammatically correct response. E.g.

There is 1 occurrence of 'foo' within input file 'bar.txt'

or

There are 12 occurences of 'foo' within input file 'bar.txt'

Conditional statements in Perl

We need to use a conditional statement to achieve this. A conditional statement tests a condition to see whether it is true or not and then executes the appropriate response.

The syntax for this is as follows:

if (CONDITION) { do something }

That is, if the condition within the parentheses evaluates to TRUE, then execute the statements within the block. Using our example above, we could code:

if ($conditions == 1)
{
print "There is 1 occurrence of 'foo' within input file 'bar.txt'";
}

But what's that double equals sign ('==') doing there, I hear you ask? That's what Perl calls a comparison operator. It is very different to the single equals sign you may have seen so far, which is know as the assignment operator. The assignment operator assigns a value to a variable. E.g.

my $no_of_bottles = 9;

The double equals comparison operator on the other hand, checks to see if the value on the right is the same as the one on the left. If so, it returns TRUE and if not, it returns FALSE.

Mixing the two is a common mistake made by beginning Perl programmers and can be difficult to debug. Let's see what would happen if we accidentally used the assignment operator in our conditional statement:

if ($conditions = 1)
{
print "There is 1 occurrence of 'foo' within input file 'bar.txt'";
}

This will always return true, because it's not actually comparing anything. That means that the code in the block immediately after the if statement will always be executed, which is not what we want to happen in this instance. Instead, if $conditions is greater than one, we want to format the output appropriately. We can use the else statement to achieve this:

if ($conditions == 1)
{
print "There is 1 occurrence of 'foo' within input file 'bar.txt'";
}
else
{
print "There are $conditions occurrences of 'foo' within input file 'bar.txt'";
}

Using the string comparison operators

That's how we test for equality in Perl with a numeric value. Perl has separate comparison operators for strings. The string equivalent to the double-equals operator is 'eq', which you use as follows:

if ($response eq 'YES')
{
print "You said YES";
}
else
{
print "I don't know what you said, but it wasn't YES";
}

Other comparison operators

Of course, you won't always want to check if one thing is equal to another. Sometimes for example, you'll want to check if a given variable contains a value which is less than or greater than a test condition. Here is a summary of Perl's comparison operators. Make sure you use the right one depending on whether you are testing a string or numeric value:

Equal to

  • Numeric: ==
  • String operator: eq

Not equal to

  • Numeric: !=
  • String operator: ne

Less than

  • Numeric:
  • String operator: lt

Greater than

  • Numeric: >
  • String operator: gt

Less than or equal to

  • Numeric:
  • String operator: le

Greater than or equal to

  • Numeric: >=
  • String operator: ge
  1. About.com
  2. Technology
  3. Perl
  4. Perl Tutorials
  5. How to Compare Values in Perl - Programming

©2014 About.com. All rights reserved.