微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Learning Perl: 10.6. Autoincrement and Autodecrement

@H_502_5@

Previous Page

Next Page

 

@H_502_5@

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 as the same operator in C and similar languages:

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

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 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 $m = 5;
    my $n = ++$m;  # increment $m to 6,and put that value into $n

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

    my $c = --$m;  # decrement $m 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 = $m++;  # $d gets the old value (5),then increment $m to 6
    my $e = $m--;  # $e gets the old value (6),then decrement $m 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) and then use the new value. If the variable is first,we return its (old) value first,and 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,[*] not using the value but only the side effect,there's no difference[

] if you put the operator before or after the variable:

[*] That is,in a void context.

[

] 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"} and 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.

@H_502_5@

Previous Page

Next Page

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐