bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] How awk keys are ordered?


From: Andrew J. Schorr
Subject: Re: [bug-gawk] How awk keys are ordered?
Date: Wed, 21 Dec 2016 17:09:08 -0500
User-agent: Mutt/1.5.23 (2014-03-12)

On Wed, Dec 21, 2016 at 03:12:19PM -0600, Peng Yu wrote:
> > Quite right. Gawk has additional optimizations for arrays
> > indexed only by integers.  Running the program with another
> > awk will definitely give different results.
> 
> I only care about gawk for now. In gawk, if the keys are numeric, does
> it guarantee to be ordered by default (at least with the current
> version of gawk).

It is not guaranteed! Try some negative numbers, for example:

bash-4.2$ gawk 'BEGIN {for (i = -5; i <= 5; i++) x[i]; for (i in x) print i}'
0
4
5
-4
-1
3
-3
-2
-5
2
1

> How does gawk treat numbers and strings differently? What if strings
> are numbers?

There are internal implementation optimizations for arrays. Good code
should not rely on the details of those optimizations, since those are
subject to change.

> > And please note that the awk spec says that array indexes are handled as
> > strings. So [i] and ["" i] refer to the same element.
> 
> I don't find where it says so in the gawk doc. Would you please let me know?

It's in the POSIX awk specification. And see section 8.2 of the gawk manual:

https://www.gnu.org/software/gawk/manual/html_node/Numeric-Array-Subscripts.html

   An important aspect to remember about arrays is that array subscripts are
   always strings. When a numeric value is used as a subscript, it is converted
   to a string value before being used for subscripting (see Conversion). This
   means that the value of the predefined variable CONVFMT can affect how your
   program accesses elements of an array. For example: 

Regards,
Andy



reply via email to

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