加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Learning Perl: 10.6. Autoincrement and Autodecrement

发布时间:2020-12-15 20:55:33 所属栏目:大数据 来源:网络整理
导读:? 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 operat

Previous Page

Next Page

?

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.

Previous Page

Next Page

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读