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: david kerns
Subject: Re: [bug-gawk] Bug: invalid evaluation of variable in for statement
Date: Tue, 2 Feb 2016 15:22:43 -0700

I'd be hard pressed to call this a bug. While awk tries to do the right thing, 
if you the programmer know you need a specific evaluation (number vs string) 
you're supposed to add 0 or concat a "" appropriately.

Sent from my iPhone

> On Feb 2, 2016, at 12:07 PM, Wolfgang Laun <address@hidden> 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.
> 
> Regards
> Wolfgang
> 
> function demo( ) {
>     print "demo";
>     split("", tosigarr);    # init
>     tosigarr[1] = 12;
>     tosigarr[2] = 23;
>     nsig = 2;
>  
>     tohsizeparam = "10v,13s";
>     gsub( / /, "", tohsizeparam );
>     hsnr = split(tohsizeparam, hashsizes, ",");
>     for( hsidx=1; hsidx <= hsnr; hsidx++ ){
> 
>         gsub(/[^0-9]*$/, "", hashsizes[hsidx]);
> 
>         print "hashsizes[" hsidx "]=" hashsizes[hsidx] "!";
> 
>         split("", tosighash);    # init
>         for( idx=1; idx <= nsig; idx++ ) {
>             h = tosigarr[idx] % hashsizes[hsidx];
>             tosighash[h] = "" tosighash[h] tosigarr[idx] " ";
>         }
> 
>         print "hashsizes[" hsidx "]=" (hashsizes[hsidx]+1) "!";
> 
> ### need to force conversion to number for the loop to work correctly
>     for( i=0; i < hashsizes[hsidx]; i++ ){
> ###    for( i=0; i < hashsizes[hsidx] + 0; i++ ){
>         if( i in tosighash ) {
>         hstr = hstr "  " tosighash[i] ",\n";
>         hused++;
>         } else {
>         hstr = hstr "  -1,\n";
>         }
>     }
>         print hstr;
>     }
> }
> 
> BEGIN {
>     demo();
> }
> 



reply via email to

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