bug-gawk
[Top][All Lists]
Advanced

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

Re: unassigned/untyped behaviour


From: arnold
Subject: Re: unassigned/untyped behaviour
Date: Wed, 22 Nov 2023 08:00:19 -0700
User-agent: Heirloom mailx 12.5 7/5/10

Hi.

Just remember Andy, really, it's all just voodoo.  :-)  :-)

That said, let's answer your question.

"Andrew J. Schorr" <aschorr@telemetry-investments.com> wrote:

> > > On 2nd thought, I don't understand why the doc is wrong. Doesn't the 
> > > statement
> > > "a[1]" actually instantiate a[1] as an unassigned scalar value?
                                                        ^^^^^^
> > 
> > No, it creates a[1] as an *untyped* value, neither array nor scalar.
> > "unassigned" IS scalar. "untyped" is neither fish nor fowl, until
> > we see how it gets used.
> > 
> > > In other words, is it proper to do this?
> > >
> > > gawk 'BEGIN {a[0]; a[0][1] = 5; print a[0][1]}'
>
> Well, this is news to me. I have a zillion scripts that instantiate
> array entries like this. For example:
>
> {
>    found[$0]
> }
>
> END {
>    for (i in found)
>       <blah blah blah>
> }
>
> I always understood that reference to "found[$0]" to mean that the
> array element was being created with the value set to the Null value
> (Nnull_string).

That is indeed how it used to work. And then we added real subarrays.

Remember, an array element has two components: the index, and the value
associated with that index.  The latter now has type Node_elem_new, which
means it can morph into a scalar if used that way the first time, or it
can morph into a subarray if used that way the first time.

But the code above was and remains perfectly valid.

> But now you're saying that it's something much more vague than that.
> Is my code snippet above considered valid? Am I supposed to instead
> be saying:
>
>    found[$0] = ""
> ?

No. As soon as you treat found[$0] as a scalar, it morphs into one.

> In section 8.1.2, "Referring to an Array Element", it says:
>
> https://www.gnu.org/software/gawk/manual/html_node/Reference-to-Elements.html
>
> "A reference to an array element that has no recorded value yields a
> value of "", the null string. This includes elements that have not been
> assigned any value as well as elements that have been deleted (see The
> delete Statement).
> 
> NOTE: A reference to an element that does not exist automatically creates
> that array element, with the null string as its value. (In some cases,
> this is unfortunate, because it might waste memory inside awk.)"
>
> So is that wrong?

It used to be exactly correct. Now, well, let's say it's a simplification
that applies to most use cases.  The association of the null string as
the value simply happens later, as soon as the value is needed, instead of
happening when the element is created.

Does that help?

Arnold



reply via email to

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