Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

awk ' BEGIN{ a[1]=1; delete a ; a=1 } '

49 views
Skip to first unread message

Hongyi Zhao

unread,
Nov 1, 2016, 8:45:02 PM11/1/16
to
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?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Kenny McCormack

unread,
Nov 1, 2016, 8:51:27 PM11/1/16
to
In article <nvbcud$vp0$1...@aspen.stu.neva.ru>,
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?

Switch to TAWK.

I'm assuming the above is GAWK - that looks like a GAWK error message.
GAWK insists that any given name be either a scalar or an array -
throughout the program.

TAWK, OTOH, is more flexible.

--
Modern Conservative: Someone who can take time out from flashing her
wedding ring around and bragging about her honeymoon to complain that a
fellow secretary who keeps a picture of her girlfriend on her desk is
"flauting her sexuality" and "forcing her lifestyle down our throats".

Hongyi Zhao

unread,
Nov 1, 2016, 8:57:52 PM11/1/16
to
On Wed, 02 Nov 2016 00:51:26 +0000, Kenny McCormack wrote:

> Switch to TAWK.
>
> I'm assuming the above is GAWK - that looks like a GAWK error message.

Yes.

> GAWK insists that any given name be either a scalar or an array -
> throughout the program.

Thanks for your notes.

>
> TAWK, OTOH, is more flexible.

Which is more powerful, gawk and tawk?

Kenny McCormack

unread,
Nov 1, 2016, 9:41:43 PM11/1/16
to
In article <nvbdmf$h8$1...@aspen.stu.neva.ru>,
Hongyi Zhao <hongy...@gmail.com> wrote:
>On Wed, 02 Nov 2016 00:51:26 +0000, Kenny McCormack wrote:
>
>> Switch to TAWK.
>>
>> I'm assuming the above is GAWK - that looks like a GAWK error message.
>
>Yes.
>
>> GAWK insists that any given name be either a scalar or an array -
>> throughout the program.
>
>Thanks for your notes.
>
>>
>> TAWK, OTOH, is more flexible.
>
>Which is more powerful, gawk and tawk?

Obviously, TAWK is the best.

As is usual, though, things that are the best, are hard to get (if you
don't already have it).

--
Watching ConservaLoons playing with statistics and facts is like watching a
newborn play with a computer. Endlessly amusing, but totally unproductive.

Kaz Kylheku

unread,
Nov 1, 2016, 10:17:20 PM11/1/16
to
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]".

Marc de Bourget

unread,
Nov 2, 2016, 6:48:04 AM11/2/16
to
Although it works with TAWK, the TAWK manual page 58 advise against to do so:
"Do not use the same name for a normal variable and an array, or for two
different arrays with different numbers of subscripts. For example:
a = 1
a[2] = 3 # NO
a[4][5] = 6 # NO

a = 1
b[2] = 3 # yes
c[4][5] = 6 # yes

While it is legal to change the number of array subscripts in an array, as
in the first example, it is not recommended because the previous contents
of the variable are lost (lost to the variable, that is: TAWK always
recovers the memory storage) and you get an entirely new array."

0 new messages