On 2016-11-02, Hongyi Zhao <
hongy...@gmail.com> wrote:
> Hi all,
>
> See the following test:
>
> $ awk ' BEGIN{ a[1]=1; delete a ; a=1 } '
> awk: cmd. line:1: fatal: attempt to use array `a' in a scalar context
>
> I've deleted the array `a', and awk still prohibits me to reuse `a' as a
> scalar name. Any hints?
Support for this is unnecesary because:
1. Awk is not an image-based language. You do not sit in an interactive
listener for an extended time, where you need to re-define functions
and variables without exiting the image. A fresh instance of awk is
run non-interactively each time you test the program.
2. You shouldn't write more than about a page of code to make an Awk
program. If you can't write a page-long program without having
to redefine an array to a scalar somewhere in that program to
solve whatever problem, maybe you should find some other hobby.
Lots of languages don't allow run-time redefining of objects
or functions. Oh, like C. (Short of advanced things like
loading/unloading dynamic libraries or modules.)
If some "int a[10];" is defined in the Linux kernel and
compiling into the vmlinux image, you don't get to redefine
that to "int a" without recompiling and rebooting into it.
If you do that in a module, you have to recompile, unload
and reload the entire module. Yet, somehow, it's not a showstopping
problem.
If you've decided that a is an array, then just keep a an array.
If you need a scalar variable, use some other identifier; don't
use a?
Many useful Awk programs do a "delete a" to scrub prior contents,
in preparation to putting more things into a[].
Lastly, did you read the GNU Awk manual about what "delete a" actually
means? It doesn't mean "delete the variable binding a so that a
becomes a free symbol".
Here is a quote:
"All the elements of an array may be deleted with a single statement by
leaving off the subscript in the delete statement, as follows:
delete array
Using this version of the delete statement is about three times more
efficient than the equivalent loop that deletes each element one at a
^^^^^^^^^^^^^^^
time."
It deletes the *elements*, not the array itself. And is equivalent
to looping over the indices and doing individual deletes!
This feature is not described by POSIX; POSIX awk only has
"delete a[index]".