# forest.awk
BEGIN {
SYMTAB["tree_status"] = "fallen"
}
But, alas, nobody was there to hear it, because a variable called
tree_status is not referenced in the code.
At first glance, it seems not wrong to do the assignment *in case* such
a variable use appears. Today, there might be no test for the field
$madonna, but it could be added tomorrow, and so the line SYMTAB[$i]=i
will update the madonna variable with the field number.
The one thing that is naive about this SYMTAB approach is that
when you do
SYMTAB[$i] = i
in a loop over the fields of the first record, you're allowing whoever
controls the contents of that record to trash arbitrary variables
in your code. (Luckily, just with an integer
value, and not a datum of the attacker's choice).
It really looks like a bad idea to pull strings from the input data,
look for variables which match those strings, and clobber those
variables.
If you do this, it's probably a good idea to namespace the
variables with a prefix. A short one might do, like f_ for
field:
# catenation of "f_" "foo", indexes into SYMTAB[]:
#
$ ./gawk 'BEGIN { SYMTAB["f_" "foo"] = 3; print fld_foo; }'
3
So this would be used like
SYMTAB["f_" $i] = i;
# ...
if $f_year > 2007 ... # not $year!