bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Assigning unpredefined SYMTAB index


From: arnold
Subject: Re: Assigning unpredefined SYMTAB index
Date: Fri, 17 Jun 2022 02:37:48 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hi Miguel.

Thanks for the report.  This bug was reported a little while ago
(considerably less politely, unfortunately).  It is fixed in the current
code base in Git, and I think I even added a test.

Much thanks!

Arnold

"Miguel Pineiro Jr." <mpj@pineiro.cc> wrote:

> Hello, everyone.
>
> Fedora 36
> Kernel 5.17.13-300.fc36.x86_64 
> GNU Awk 5.1.1, API: 3.1 (GNU MPFR 4.1.0-p13, GNU MP 6.2.1)
>
> The gawk manual states [1] that as of gawk 5.0 an attempt to assign
> to SYMTAB[index] when index isn't already defined produces a fatal
> error. This isn't completely accurate. It can be done for an array.
> 
> As documented, assignment to SYMTAB["a"] fails fatally when a is
> undefined, but assignment to SYMTAB["a"]["b"] succeeds and creates the
> array SYMTAB["a"] (but without creating a global variable).
>
> gawk 'BEGIN {
>     if (!("a" in SYMTAB))
>         print "a is not predefined";
>     SYMTAB["a"]["b"] = 123;
>     if ("a" in SYMTAB)
>         print "SYMTAB[\"a\"] array exists, length:", length(SYMTAB["a"]);
>     if ("b" in SYMTAB["a"])
>         print "SYMTAB[\"a\"][\"b\"] exists: " SYMTAB["a"]["b"];
> }'
>
> Output:
> a is not predefined
> SYMTAB["a"] array exists, length: 1
> SYMTAB["a"]["b"] exists: 123
>
>
> The array can even be named SYMTAB, but SYMTAB["SYMTAB"] isn't
> self-referential. This array's membership seems to have nothing to do
> with the global variable namespace.
>
> gawk 'BEGIN {
>     a=1;
>     SYMTAB["SYMTAB"]["a"] = 2;
>     print a;
>     print SYMTAB["a"];
>     print SYMTAB["SYMTAB"]["a"];
> }'
>
> Output:
> 1
> 1
> 2
>
> This hasn't caused me any problems and none of my "real" scripts are
> affected. I just came across this while working on nawk bugs and comparing
> behavior across implementations.
>
> If this isn't a bug, please excuse the distraction.
>
> Take care,
> Miguel
>
> [1] 
> https://www.gnu.org/software/gawk/manual/html_node/Auto_002dset.html#index-differences-in-awk-and-gawk-39



reply via email to

[Prev in Thread] Current Thread [Next in Thread]