bug-gawk
[Top][All Lists]
Advanced

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

[bug-gawk] array initialization


From: Andrew J. Schorr
Subject: [bug-gawk] array initialization
Date: Fri, 4 Jan 2019 17:28:02 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

A nice feature of gawk is that it has optimized array types for the cases where
the subscripts are integers (int_array.c) or positive integers (cint_array.c).
Gawk decides which backend array implementation to use based on the type of the
first index inserted into the array (see array.c:null_lookup).

In the gawk man page, it says:
       NOTE:  You may need to tell gawk that an array element is really a sub‐
       array in order to use it where gawk expects an array (such  as  in  the
       second argument to split()).  You can do this by creating an element in
       the subarray and then deleting it with the delete statement.
But if one actually follows this recommendation, one will have chosen
a backend array implementation that may not be optimal for your code.

One could initialize the array y by saying:
   y[0] = 0
   delete y[0]
But in so doing, one has (inadvertently?) selected a backend "cint" array 
implementation.

Or one could say:
   y[-1] = 0
   delete y[-1]
And this would choose the "int" array implementation.

Or:
   y[""] = 0
   delete y[""]
And you'd have a string array.

Based on some debugging, I believe one can actually get a "null" (typeless) 
array
like so:
   split("", y)
So that appears to be the optimal way to initialize an array variable. Should
we document this as the recommended approach? While this concept of
initializing arrays is mentioned in the man page, I can't seem to find any
documentation about this in the texinfo manual...

Note: it would be nice if there were some way for user code to discern what 
type of
array implementation is being used. For my testing, I hacked the typeof function
to print some additional info for array types. Should we consider adding an 
optional
2nd argument to typeof that would tell it to return more detailed information 
about
the array type?

I may have the flu at the moment, so please excuse me if this message is 
incoherent.

Regards,
Andy



reply via email to

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