bug-gawk
[Top][All Lists]
Advanced

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

Re: the wonder of "cannot assign to arbitrary elements"


From: Manuel Collado
Subject: Re: the wonder of "cannot assign to arbitrary elements"
Date: Sun, 12 Dec 2021 20:18:39 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0

El 12/12/2021 a las 19:52, Denis Shirokov escribió:
BEGIN{
        typeof( a )
        SYMTAB[ "a" ] = 1
        SYMTAB[ "b" ] = 2                                     # <- here will be 
an arbitrary element erro
}

This is on purpose.


could someone please inform me what's the difference between a and b ?
and how to avoid an error with 'a' ?
------------------------------->-'b', I suppose.

Variables are created when the awk script is parsed, not when it is executed. "a" is the name of a variable, and hence there is a corresponding SYMTAB entry.

"b" is just an invalid index of SYMTAB. Invalid because there is no variable with such name.


feature of accessing the content of the global variables for writes is useless.

You can *assign* values to variables via SYMTAB, but you can not use SYMTAB to *create* variables. To create a variable, just mention it in the script code.

gawk '
BEGIN{
  typeof( a )
  b
  SYMTAB[ "a" ] = 1
  SYMTAB[ "b" ] = 2    # <- No error here.
  print b
}'

2

Works as expected.



commonly - the fatal "cannot assign to arbitrary elements" - is idea that's
does more harm than good. it's practically nullifies feature of write
access to the content
of global variables because it is better to perform direct value
writing instead of trying
to understand why(and where) gawk determinates an element as an
arbitrary element of SYMTAB.

as a gawk user i may say that without fatal "cannot assign to
arbitrary elements" - life was been really easier and nice
and gawk was been more flexible.

It was in the past. But with a different, confusing semantics. So arbitrary SYMTAB indexes were finally forbidden.


Kind Regrads
Denis

HTH. Regards.

--
Manuel Collado - http://mcollado.z15.es



reply via email to

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