bug-gawk
[Top][All Lists]
Advanced

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

Re: unassigned/untyped behaviour


From: Andrew J. Schorr
Subject: Re: unassigned/untyped behaviour
Date: Wed, 22 Nov 2023 09:24:54 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

On Tue, Nov 21, 2023 at 09:33:52PM -0700, arnold@skeeve.com wrote:
> "Andrew J. Schorr" <aschorr@telemetry-investments.com> wrote:
> 
> > > >           For example:
> > > >
> > > >                BEGIN {
> > > >                    # creates a[1] but it has no assigned value
> > > >                    a[1]
> > > >                    print typeof(a[1])  # unassigned
> > > >                }
> > > >
> > > > But in fact, that's not what the master branch produces:
> > > 
> > > That example is bad. I will fix the doc.
> >
> > 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).

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] = ""
?

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?

Regards,
Andy



reply via email to

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