gawk: Increment Ops
1
1 6.2.4 Increment and Decrement Operators
1 ---------------------------------------
1
1 "Increment" and "decrement operators" increase or decrease the value of
1 a variable by one. An assignment operator can do the same thing, so the
1 increment operators add no power to the 'awk' language; however, they
1 are convenient abbreviations for very common operations.
1
1 The operator used for adding one is written '++'. It can be used to
1 increment a variable either before or after taking its value. To
1 "pre-increment" a variable 'v', write '++v'. This adds one to the value
1 of 'v'--that new value is also the value of the expression. (The
1 assignment expression 'v += 1' is completely equivalent.) Writing the
1 '++' after the variable specifies "post-increment". This increments the
1 variable value just the same; the difference is that the value of the
1 increment expression itself is the variable's _old_ value. Thus, if
1 'foo' has the value four, then the expression 'foo++' has the value
1 four, but it changes the value of 'foo' to five. In other words, the
1 operator returns the old value of the variable, but with the side effect
1 of incrementing it.
1
1 The post-increment 'foo++' is nearly the same as writing '(foo += 1)
1 - 1'. It is not perfectly equivalent because all numbers in 'awk' are
1 floating point--in floating point, 'foo + 1 - 1' does not necessarily
1 equal 'foo'. But the difference is minute as long as you stick to
1 numbers that are fairly small (less than 10e12).
1
1 Fields and array elements are incremented just like variables. (Use
1 '$(i++)' when you want to do a field reference and a variable increment
1 at the same time. The parentheses are necessary because of the
1 precedence of the field reference operator '$'.)
1
1 The decrement operator '--' works just like '++', except that it
1 subtracts one instead of adding it. As with '++', it can be used before
1 the lvalue to pre-decrement or after it to post-decrement. Following is
1 a summary of increment and decrement expressions:
1
1 '++LVALUE'
1 Increment LVALUE, returning the new value as the value of the
1 expression.
1
1 'LVALUE++'
1 Increment LVALUE, returning the _old_ value of LVALUE as the value
1 of the expression.
1
1 '--LVALUE'
1 Decrement LVALUE, returning the new value as the value of the
1 expression. (This expression is like '++LVALUE', but instead of
1 adding, it subtracts.)
1
1 'LVALUE--'
1 Decrement LVALUE, returning the _old_ value of LVALUE as the value
1 of the expression. (This expression is like 'LVALUE++', but
1 instead of adding, it subtracts.)
1
1 Operator Evaluation Order
1
1 Doctor, it hurts when I do this!
1 Then don't do that!
1 -- _Groucho Marx_
1
1 What happens for something like the following?
1
1 b = 6
1 print b += b++
1
1 Or something even stranger?
1
1 b = 6
1 b += ++b + b++
1 print b
1
1 In other words, when do the various side effects prescribed by the
1 postfix operators ('b++') take effect? When side effects happen is
1 "implementation-defined". In other words, it is up to the particular
1 version of 'awk'. The result for the first example may be 12 or 13, and
1 for the second, it may be 22 or 23.
1
1 In short, doing things like this is not recommended and definitely
1 not anything that you can rely upon for portability. You should avoid
1 such things in your own programs.
1