There is no way to make a variable local to a { ... }
block in
awk, but you can make a variable local to a function. It is
good practice to do so whenever a variable is needed only in that
function.
To make a variable local to a function, simply declare the variable as
an argument after the actual function arguments
(see Definition Syntax).
Look at the following example where variable
i
is a global variable used by both functions foo()
and
bar()
:
function bar() { for (i = 0; i < 3; i++) print "bar's i=" i } function foo(j) { i = j + 1 print "foo's i=" i bar() print "foo's i=" i } BEGIN { i = 10 print "top's i=" i foo(0) print "top's i=" i }
Running this script produces the following, because the i
in
functions foo()
and bar()
and at the top level refer to the same
variable instance:
top's i=10 foo's i=1 bar's i=0 bar's i=1 bar's i=2 foo's i=3 top's i=3
If you want i
to be local to both foo()
and bar()
do as
follows (the extra-space before i
is a coding convention to
indicate that i
is a local variable, not an argument):
function bar( i) { for (i = 0; i < 3; i++) print "bar's i=" i } function foo(j, i) { i = j + 1 print "foo's i=" i bar() print "foo's i=" i } BEGIN { i = 10 print "top's i=" i foo(0) print "top's i=" i }
Running the corrected script produces the following:
top's i=10 foo's i=1 bar's i=0 bar's i=1 bar's i=2 foo's i=1 top's i=10