manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Assignment Ops

 
 6.2.3 Assignment Expressions
 ----------------------------
 
 An "assignment" is an expression that stores a (usually different) value
 into a variable.  For example, let's assign the value one to the
 variable 'z':
 
      z = 1
 
    After this expression is executed, the variable 'z' has the value
 one.  Whatever old value 'z' had before the assignment is forgotten.
 
    Assignments can also store string values.  For example, the following
 stores the value '"this food is good"' in the variable 'message':
 
      thing = "food"
      predicate = "good"
      message = "this " thing " is " predicate
 
 This also illustrates string concatenation.  The '=' sign is called an
 "assignment operator".  It is the simplest assignment operator because
 the value of the righthand operand is stored unchanged.  Most operators
 (addition, concatenation, and so on) have no effect except to compute a
 value.  If the value isn't used, there's no reason to use the operator.
 An assignment operator is different; it does produce a value, but even
 if you ignore it, the assignment still makes itself felt through the
 alteration of the variable.  We call this a "side effect".
 
 Variables::); it can also be a field (⇒Changing Fields) or an
 array element (⇒Arrays).  These are all called "lvalues", which
 means they can appear on the lefthand side of an assignment operator.
 The righthand operand may be any expression; it produces the new value
 that the assignment stores in the specified variable, field, or array
 element.  (Such values are called "rvalues".)
 
    It is important to note that variables do _not_ have permanent types.
 A variable's type is simply the type of whatever value was last assigned
 to it.  In the following program fragment, the variable 'foo' has a
 numeric value at first, and a string value later on:
 
      foo = 1
      print foo
      foo = "bar"
      print foo
 
 When the second assignment gives 'foo' a string value, the fact that it
 previously had a numeric value is forgotten.
 
    String values that do not begin with a digit have a numeric value of
 zero.  After executing the following code, the value of 'foo' is five:
 
      foo = "a string"
      foo = foo + 5
 
      NOTE: Using a variable as a number and then later as a string can
      be confusing and is poor programming style.  The previous two
      examples illustrate how 'awk' works, _not_ how you should write
      your programs!
 
    An assignment is an expression, so it has a value--the same value
 that is assigned.  Thus, 'z = 1' is an expression with the value one.
 One consequence of this is that you can write multiple assignments
 together, such as:
 
      x = y = z = 5
 
 This example stores the value five in all three variables ('x', 'y', and
 'z').  It does so because the value of 'z = 5', which is five, is stored
 into 'y' and then the value of 'y = z = 5', which is five, is stored
 into 'x'.
 
    Assignments may be used anywhere an expression is called for.  For
 example, it is valid to write 'x != (y = 1)' to set 'y' to one, and then
 test whether 'x' equals one.  But this style tends to make programs hard
 to read; such nesting of assignments should be avoided, except perhaps
 in a one-shot program.
 
    Aside from '=', there are several other assignment operators that do
 arithmetic with the old value of the variable.  For example, the
 operator '+=' computes a new value by adding the righthand value to the
 old value of the variable.  Thus, the following assignment adds five to
 the value of 'foo':
 
      foo += 5
 
 This is equivalent to the following:
 
      foo = foo + 5
 
 Use whichever makes the meaning of your program clearer.
 
    There are situations where using '+=' (or any assignment operator) is
 _not_ the same as simply repeating the lefthand operand in the righthand
 expression.  For example:
 
      # Thanks to Pat Rankin for this example
      BEGIN  {
          foo[rand()] += 5
          for (x in foo)
             print x, foo[x]
 
          bar[rand()] = bar[rand()] + 5
          for (x in bar)
             print x, bar[x]
      }
 
 The indices of 'bar' are practically guaranteed to be different, because
 'rand()' returns different values each time it is called.  (Arrays and
 the 'rand()' function haven't been covered yet.  ⇒Arrays, and
 ⇒Numeric Functions for more information.)  This example
 illustrates an important fact about assignment operators: the lefthand
 expression is only evaluated _once_.
 
    It is up to the implementation as to which expression is evaluated
 first, the lefthand or the righthand.  Consider this example:
 
      i = 1
      a[i += 2] = i + 1
 
 The value of 'a[3]' could be either two or four.
 
    ⇒Table 6.2 table-assign-ops. lists the arithmetic assignment
 operators.  In each case, the righthand operand is an expression whose
 value is converted to a number.
 
 Operator               Effect
 --------------------------------------------------------------------------
 LVALUE '+='            Add INCREMENT to the value of LVALUE.
 INCREMENT
 LVALUE '-='            Subtract DECREMENT from the value of LVALUE.
 DECREMENT
 LVALUE '*='            Multiply the value of LVALUE by COEFFICIENT.
 COEFFICIENT
 LVALUE '/=' DIVISOR    Divide the value of LVALUE by DIVISOR.
 LVALUE '%=' MODULUS    Set LVALUE to its remainder by MODULUS.
 LVALUE '^=' POWER      Raise LVALUE to the power POWER.
 LVALUE '**=' POWER     Raise LVALUE to the power POWER.  (c.e.)
 
 Table 6.2: Arithmetic assignment operators
 
      NOTE: Only the '^=' operator is specified by POSIX. For maximum
      portability, do not use the '**=' operator.
 
       Syntactic Ambiguities Between '/=' and Regular Expressions
 
    There is a syntactic ambiguity between the '/=' assignment operator
 and regexp constants whose first character is an '='.  (d.c.)  This is
 most notable in some commercial 'awk' versions.  For example:
 
      $ awk /==/ /dev/null
      error-> awk: syntax error at source line 1
      error->  context is
      error->         >>> /= <<<
      error-> awk: bailing out at source line 1
 
 A workaround is:
 
      awk '/[=]=/' /dev/null
 
    'gawk' does not have this problem; BWK 'awk' and 'mawk' also do not.
 
© manpagez.com 2000-2018
Individual documents may contain additional copyright information.