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

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

gawk version 3.1.3 and passing a 1-d array into a function + a bug ? + a


From: Brocci, R. A.
Subject: gawk version 3.1.3 and passing a 1-d array into a function + a bug ? + an enhancement
Date: Fri, 7 Nov 2008 14:00:06 -0500

 To  Arnold Robbinsm or gawk maintainer(s),
 
I owe you an apology -- assuming you received my e-mail of 05 Nov, a
copy of which appears below.
 
Shortly after sending this message, I discovered that it appears that I
have a "function placement" issue, rather than with the function itself.
 
That is, the original e-mail message was issued for an ~ 40-record
 
    function someName ( N, X )  {
        for ( I = 1 ; I <= N ; I ++ )  {
            do my stuff ;
        }
        return ;
    }
 
construct that was at ~ record 700 out of a ~3200 record awk procedure,
say real.awk, which bombed with the "trying to use the scalar X as an
array" diagnostic, and which led me to send the  05 Nov e-mail.
 
But, when the someName function was placed in play.awk, which is ~ 100
records, no diagnostic was issued and it behaved as I expected for N <
11.  
 
And to further confuse me, when I placed someName as the first function
encountered in real.awk, the diagnostic disappeared -- but I haven't yet
tried to put real.awk through its paces, simply because I'm not
comfortable with these observations.
 
Note: My "awk-long" habit has been to place my functions in alphanumeric
order by name at the top of my file, and to then issue the illustrative
 
    awk  [-F ...] -f real.awk [-v ...=...] myFile(s)
 
command.
 
Possible bug: In the process of this effort, I did encounter what I
believe is a bug in version 3.1.3 and 3.1.5. Namely, I played with the
 
    {
        if( $0 == ""  ||  match($0,/^[ ]*$/) > 0 )  next ;
        n = split( $0, A, "," ) ;
        i = 0 ;
        while ( i < n )  { X[++i] = A[i] ; print i,A[i],X[i],i ; } 
    }
 
construct, where the { X... } notation is due only to the presence of
the print statement, which shows that X[1]appears to be A[0], X[2] =
A[1], etc. (The obvious fix to the above was to use { i++; X[i]... }.)
 
Or, have I just tumbled to a violation of the standard after all these
years?
 
An enhancement: The exitNow command say, whose behavior is like the
Fortran stop statement or the Perl die command.
 
Again, thanks for your time.
 
 
 
 
Tony Brocci   07 Nov 2008
KAPL, Inc.
Schenectsady, NY  12301
518-395-6682
 
05 Nov 2008 e-mail follows
 
Arnold,
 
Below is an e-mail I originally sent this morning to address@hidden
<mailto:address@hidden> , and the reply (from Jeanne Rasata) led me to you.
 
Gnu,
 
I think this should go to Karl Berry, but I'm not sure of his address.
 
Is there any "notation" that lets me inform the parser that in the
 
    function someName ( N, X )  {
        for ( I = 1; ... )  {
            Y = X[I] ;
            ...
        }
    }
 
construct, X is an array of size N, so as to preclude the error
diagnostic about trying to use the scalar X as an array?
 
I've tried various combinations of the ( N, X[...] ) notation, even
though I expected them to fail.
 
A potential solution that seems to work is to make X "global" as in
 
    asort( myArray, X ) ;
    someName( numXs ) ;
 
but I've not yet tried it in all situations of immediate interest -- not
to mention that I really don't want to operate on a sorted version of
myArray.
 
So, if I must use the
 
    for ( i = 1; ... )  {
        X[i] = myArray[i] ;
    }
    someName( numXs ) ;
 
construct to adhere to my "requirements," perhaps I might do well to
just imbed the someName functionality in the for loop itself .
 
Any assistance you can provide will be welcome.
 

Tony Brocci   05 Nov 2008 



reply via email to

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