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.Ctrl-V
, Ctrl-J
) to demonstrate line-numbers below.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.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).one-true-awk
and Linux's GNU awk (aka gawk
):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: