Pre-declaring Arrays
Using an array operation on variables intended to later be used as arrays in your awk
BEGIN
statements can make debugging larger scripts easier, especially if multiple people are working on the same code. Shorter scripts and one-liners probably shouldn't bother pre-declaring arrays as-described.
Attempting to assign a value to a scalar variable that has the same name as an array will produce an error message.
On Linux:
All of these error messages are to be expected and make for quicker diagnosis of problems.
FreeBSD does even better, adding line numbers and pre-scanning.
NOTE: I've separated the two statements with a literal newline (Ctrl-V
, Ctrl-J
) to demonstrate line-numbers below.
Something to notice about the first command is that, unlike Linux's awk
, the error raised is not about line 2 trying to assign to an existing scalar, but an error is raised about the previous line assigning a scalar value to what will eventually be used as an array. This is because FreeBSD's awk
[https://svnweb.freebsd.org/base/head/contrib/one-true-awk/] pre-scans all namespaces across the entire script before executing.
Attempting to separate the namespace by using a function doesn't work because the scalar assignment in the BEGIN { ... }
block creates a global scalar that collides with any local namespaces. Neither one-true-awk
nor gawk
allow this (a good thing) and prevent the execution of the script. Both versions prematurely terminate and leave the input(s) untouched (which may be important if you've programmed retries and have the ability to recover somehow).
Using delete to pre-declare an array is supported by both FreeBSD's one-true-awk
and Linux's GNU awk (aka gawk
):
If you have to support Operating Systems with different awk
implementations (for example, nawk
and mawk
), you should be able to use the more portable but perhaps less obvious idiom for pre-declaring an array:
Last updated