gawk: Variable Scope

1 
1 9.2.3.2 Controlling Variable Scope
1 ..................................
1 
1 Unlike in many languages, there is no way to make a variable local to a
1 '{' ... '}' block in 'awk', but you can make a variable local to a
1 function.  It is good practice to do so whenever a variable is needed
1 only in that function.
1 
1    To make a variable local to a function, simply declare the variable
11 as an argument after the actual function arguments (⇒Definition
 Syntax).  Look at the following example, where variable 'i' is a
1 global variable used by both functions 'foo()' and 'bar()':
1 
1      function bar()
1      {
1          for (i = 0; i < 3; i++)
1              print "bar's i=" i
1      }
1 
1      function foo(j)
1      {
1          i = j + 1
1          print "foo's i=" i
1          bar()
1          print "foo's i=" i
1      }
1 
1      BEGIN {
1            i = 10
1            print "top's i=" i
1            foo(0)
1            print "top's i=" i
1      }
1 
1    Running this script produces the following, because the 'i' in
1 functions 'foo()' and 'bar()' and at the top level refer to the same
1 variable instance:
1 
1      top's i=10
1      foo's i=1
1      bar's i=0
1      bar's i=1
1      bar's i=2
1      foo's i=3
1      top's i=3
1 
1    If you want 'i' to be local to both 'foo()' and 'bar()', do as
1 follows (the extra space before 'i' is a coding convention to indicate
1 that 'i' is a local variable, not an argument):
1 
1      function bar(    i)
1      {
1          for (i = 0; i < 3; i++)
1              print "bar's i=" i
1      }
1 
1      function foo(j,    i)
1      {
1          i = j + 1
1          print "foo's i=" i
1          bar()
1          print "foo's i=" i
1      }
1 
1      BEGIN {
1            i = 10
1            print "top's i=" i
1            foo(0)
1            print "top's i=" i
1      }
1 
1    Running the corrected script produces the following:
1 
1      top's i=10
1      foo's i=1
1      bar's i=0
1      bar's i=1
1      bar's i=2
1      foo's i=1
1      top's i=10
1 
1    Besides scalar values (strings and numbers), you may also have local
1 arrays.  By using a parameter name as an array, 'awk' treats it as an
1 array, and it is local to the function.  In addition, recursive calls
1 create new arrays.  Consider this example:
1 
1      function some_func(p1,      a)
1      {
1          if (p1++ > 3)
1              return
1 
1          a[p1] = p1
1 
1          some_func(p1)
1 
1          printf("At level %d, index %d %s found in a\n",
1               p1, (p1 - 1), (p1 - 1) in a ? "is" : "is not")
1          printf("At level %d, index %d %s found in a\n",
1               p1, p1, p1 in a ? "is" : "is not")
1          print ""
1      }
1 
1      BEGIN {
1          some_func(1)
1      }
1 
1    When run, this program produces the following output:
1 
1      At level 4, index 3 is not found in a
1      At level 4, index 4 is found in a
1 
1      At level 3, index 2 is not found in a
1      At level 3, index 3 is found in a
1 
1      At level 2, index 1 is not found in a
1      At level 2, index 2 is found in a
1