bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] typeof() array subscripts


From: Andrew J. Schorr
Subject: Re: [bug-gawk] typeof() array subscripts
Date: Sat, 20 Jun 2015 14:57:14 -0400
User-agent: Mutt/1.5.23 (2014-03-12)

On Sat, Jun 20, 2015 at 01:23:22PM +0200, Hermann Peifer wrote:
> I just came across the new builtin function typeof() in gawk master and made
> the below observations which I found confusing, because "array subscripts
> are always strings", as I learned from the manual. Maybe I am overlooking
> something.

Well, the truth is not quite as simple as the manual makes it out to be.
The code has some different backend array implementations to optimize 
performance for arrays with integer subscripts, since that's an important
usage case. 

> # These subscripts are numbers, rather than the expected strings.
> $ awk 'BEGIN{ a[1]; a[2]; for (i in a) print i, typeof(i) }'
> 1 scalar_n
> 2 scalar_n
> 
> # Same here...
> $ awk 'BEGIN{ a["1"]; a["2"]; for (i in a) print i, typeof(i) }'
> 1 scalar_n
> 2 scalar_n
> 
> # One number and one string
> $ awk 'BEGIN{ a["1"]; a["x"]; for (i in a) print i, typeof(i) }'
> x scalar_s
> 1 scalar_n
> 
> # Why 2 strings in the below case?
> $ awk 'BEGIN{ a["x"]; a["1"]; for (i in a) print i, typeof(i) }'
> x scalar_s
> 1 scalar_s

I think what you're seeing here are artifacts of that issue.  For fun, try the
same tests with "gawk -M".  I don't think it should really matter; can you
think of cases where this could cause trouble?

Regards,
Andy



reply via email to

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