bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Bug: invalid evaluation of variable in for statement


From: Andrew J. Schorr
Subject: Re: [bug-gawk] Bug: invalid evaluation of variable in for statement
Date: Wed, 3 Feb 2016 09:49:15 -0500
User-agent: Mutt/1.5.23 (2014-03-12)

On Tue, Feb 02, 2016 at 08:07:01PM +0100, Wolfgang Laun wrote:
> Since searches like  "evaluation and variable and for statement" didn't
> turn up anything, I may have found a new bug in GNU Awk 4.0.1.
> 
> A for statement like the one below doesn't work correctly:
>    for( i=0; i < hashsizes[hsidx]; i++ ){
> 
> After adding "+0" to the upper limit, it works fine. The code has worked
> fine for years, using GNU Awk 3.x. Below is the full program to demonstrate
> the problem.

Please refer to the documentation here:
   https://www.gnu.org/software/gawk/manual/html_node/Variable-Typing.html

>     tohsizeparam = "10v,13s";
>     gsub( / /, "", tohsizeparam );
>     hsnr = split(tohsizeparam, hashsizes, ",");

At this point, I think the entries in the hashsizes array
are considered to have "strnum" values.

...
>         gsub(/[^0-9]*$/, "", hashsizes[hsidx]);

I believe this gsub call changes the type to "string" instead of "strnum".

As a result, gawk thinks the "for" loop test is between a string and
a number, so it does a string comparison.

Here's an example to consider:

bash-4.3$ ./gawk --version | head -1
GNU Awk 3.1.8
bash-4.3$ echo 5 | ./gawk '{x = $1; print (x < 10); gsub(/[^0-9]*$/, "", x); 
print (x < 10)}'
1
0

bash-4.3$ ./gawk --version | head -1
GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.2, GNU MP 6.0.0)
bash-4.3$ echo 5 | ./gawk '{x = $1; print (x < 10); gsub(/[^0-9]*$/, "", x); 
print (x < 10)}'
1
0

That being said, your example does give different results in 3.1.8, so I'd have
to dig deeper to understand why.

Regards,
Andy



reply via email to

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