Book HomeLearning Perl, 3rd EditionSearch this book

10.6. Autoincrement and Autodecrement

You'll often want a scalar variable to count up or down by one. Since these are frequent constructs, there are shortcuts for them, like nearly everything else we do frequently.

The autoincrement operator ("++") adds one to a scalar variable, like the same operator in C and similar languages:

my $bedrock = 42;
$bedrock++;  # add one to $bedrock; it's now 43

Just like other ways of adding one to a variable, the scalar will be created if necessary:

my @people = qw{ fred barney fred wilma dino barney fred pebbles };
my %count;                     # new empty hash
$count{$_}++ foreach @people;  # creates new keys and values as needed

The first time through that foreach loop, $count{$_} is incremented. That's $count{"fred"}, which thus goes from undef (since it didn't previously exist in the hash) up to 1. The next time through the loop, $count{"barney"} becomes 1; after that, $count{"fred"} becomes 2. Each time through the loop, one element in %count is incremented, and possibly created as well. After that loop is done, $count{"fred"} is 3. This provides a quick and easy way to see which items are in a list and how many times each one appears.

Similarly, the autodecrement operator ("--") subtracts one from a scalar variable:

$bedrock--;  # subtract one from $bedrock; it's 42 again

10.6.1. The Value of Autoincrement

You can fetch the value of a variable and change that value at the same time. Put the ++ operator in front of the variable name to increment the variable first and then fetch its value. This is a preincrement:

my $a = 5;
my $b = ++$a;  # increment $a to 6, and put that value into $b

Or put the -- operator in front to decrement the variable first and then fetch its value. This is a predecrement:

my $c = --$a;  # decrement $a to 5, and put that value into $c

Here's the tricky part. Put the variable name first to fetch the value first, and then do the increment or decrement. This is called a postincrement or postdecrement:

my $d = $a++;  # $d gets the old value (5), then increment $a to 6
my $e = $a--;  # $e gets the old value (6), then decrement $a to 5

It's tricky because we're doing two things at once. We're fetching the value, and we're changing it in the same expression. If the operator is first, we increment (or decrement) first, then use the new value. If the variable is first, we return its (old) value first, then do the increment or decrement. Another way to say it is that these operators return a value, but they also have the side effect of modifying the variable's value.

If you write these in an expression of their own,[221] not using the value but only the side effect, there's no difference[222] whether you put the operator before or after the variable:

[221]That is, in a void context.

[222]Programmers who get inside the implementations of languages may expect that postincrement and postdecrement would be less efficient than their counterparts, but Perl's not like that. Perl automatically optimizes the post- forms when they're used in a void context.

$bedrock++;  # adds one to $bedrock
++$bedrock;  # just the same; adds one to $bedrock

A common use of these operators is in connection with a hash, to identify when an item has been seen before:

my @people = qw{ fred barney bamm-bamm wilma dino barney betty pebbles };
my %seen;

foreach (@people) {
  print "I've seen you somewhere before, $_!\n"
    if $seen{$_}++;
}

When barney shows up for the first time, the value of $seen{$_}++ is false, since it's the value of $seen{$_}, which is $seen{"barney"}, which is undef. But that expression has the side effect of incrementing $seen{"barney"}. When barney shows up again, $seen{"barney"} is now a true value, so the message is printed.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.