Learning Perl: 10.6. Autoincrement and Autodecrement
来源:互联网 发布:添加打印机无usb端口 编辑:程序博客网 时间:2024/06/01 08:09
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.
- Learning Perl: 10.6. Autoincrement and Autodecrement
- Learning Perl: 1.1. Questions and Answers
- Learning Perl: 3.7. Scalar and List Context
- Learning Perl
- Learning Perl
- Learning Perl
- Learning Perl:
- Learning Perl
- Learning perl
- Learning Perl: 1.6. Exercises
- Learning Perl: 2.1. Numbers
- Learning Perl: 2.2. Strings
- Learning Perl: 2.12. Exercises
- Learning Perl: 4.4. Arguments
- Learning Perl: 5.6. Filehandles
- Perl Learning: 8.3. Anchors
- Learning Perl: 8.8. Precedence
- Learning Perl 笔记
- 飞秋_飞秋2010_飞秋2010下载_飞秋下载2010正式版
- Learning Perl: 10.2. The until Control Structure
- Learning Perl: 10.3. Expression Modifiers
- Learning Perl: 10.4. The Naked Block Control Structure
- Learning Perl: 10.5. The elsif Clause
- Learning Perl: 10.6. Autoincrement and Autodecrement
- ASP.NET中Response.Redirect()方法深度剖析(转)
- 一些学习prestashop的好网站
- Scite配置
- jfreechart实例
- 精华贴子整理
- 如何在ListBox中添加控件后让ListBox滚动到最底部
- 正则表达式
- Flex 在线API