bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: Gawk Bug in arrays?


From: Davide Brini
Subject: Re: Gawk Bug in arrays?
Date: Sat, 6 Nov 2010 13:20:02 +0000

On Fri, 5 Nov 2010 12:11:44 -0700 (PDT) address@hidden wrote:

> Hi, I'm using Gawk 3.1.8, and if I run this:
> 
> BEGIN {
>     a[1] = "hi"
>     if ( a[2] != "" ) print a[2]
>     for ( inx in a )
>     {
>         print inx " " a[inx]
>     }
> }
> 
> I get:
> 
> bash-3.2$ gawk -f awkhate.awk
> 1 hi
> 2
> bash-3.2$
> 
> The act of comparing a[2] (which should not exist) sets it to "".  I was 
> mistakenly using this instead of a preferred:
> if ( 2 in a )
> 
> However, I don't think that it should have behaved this way, regardless.

It is expected behavior. This is required by POSIX, and documented in the
GNU awk manual:

"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 Delete). Such a
reference 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.)"

-- 
D.



reply via email to

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