[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Assigning unpredefined SYMTAB index
From: |
Miguel Pineiro Jr. |
Subject: |
Assigning unpredefined SYMTAB index |
Date: |
Thu, 16 Jun 2022 20:20:39 -0400 |
User-agent: |
Cyrus-JMAP/3.7.0-alpha0-712-gb9e94258b0-fm-20220610.001-gb9e94258 |
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
- Assigning unpredefined SYMTAB index,
Miguel Pineiro Jr. <=