Hi All,
The Gawk man page says:
> Starting with version 3.1.5, as a non-standard extension,
> with an array argument, length() returns the number
> of elements in the array.
It looks like Gawk's length(array) extension does not work inside
functions. Is this a bug or feature or am I missing something? See the
example below. I am using GNU Awk 3.1.6
$ cat testdata
CD NAME
AT Austria
BG Bulgaria
CH Switzerland
DE Germany
EE Estonia
FR France
GR Greece
$ cat test.awk
# Populate array
NR > 1 { array[$1] = $2 }
# Print array length and call function A
END { print "array:",length(array) ; A(array) }
function A(array_A) { print "array_A:", length(array_A) }
$ gawk -f test.awk testdata
array: 7
gawk: test.awk:8: (FILENAME=data FNR=8) fatal: attempt to use array
`array_A (from array)' in a scalar context
BTW, there is no such error if I have asort(array_A) or asorti(array_A)
inside the function.
Hermann
|
|
0
|
|
|
|
Reply
|
Hermann
|
3/15/2008 11:08:25 AM |
|
On 3/15/2008 6:08 AM, Hermann Peifer wrote:
> Hi All,
>
> The Gawk man page says:
> > Starting with version 3.1.5, as a non-standard extension,
> > with an array argument, length() returns the number
> > of elements in the array.
>
> It looks like Gawk's length(array) extension does not work inside
> functions. Is this a bug or feature or am I missing something? See the
> example below. I am using GNU Awk 3.1.6
>
> $ cat testdata
> CD NAME
> AT Austria
> BG Bulgaria
> CH Switzerland
> DE Germany
> EE Estonia
> FR France
> GR Greece
>
> $ cat test.awk
>
> # Populate array
> NR > 1 { array[$1] = $2 }
>
> # Print array length and call function A
> END { print "array:",length(array) ; A(array) }
>
> function A(array_A) { print "array_A:", length(array_A) }
>
> $ gawk -f test.awk testdata
> array: 7
> gawk: test.awk:8: (FILENAME=data FNR=8) fatal: attempt to use array
> `array_A (from array)' in a scalar context
>
> BTW, there is no such error if I have asort(array_A) or asorti(array_A)
> inside the function.
>
> Hermann
I get the same result with gawk 3.1.6 for cygwin. Obviously you can work around
it since asort() returns the number of elements in an array just like length()
is supposed to (or "for (i in array) lgth++" if you don't want to be
gawk-specific) but it does seem like a bug. Anyone know if there's a list of
known gawk bugs on-line somewhere?
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
3/15/2008 1:02:03 PM
|
|
Ed Morton wrote:
>
> On 3/15/2008 6:08 AM, Hermann Peifer wrote:
>> Hi All,
>>
>> The Gawk man page says:
>> > Starting with version 3.1.5, as a non-standard extension,
>> > with an array argument, length() returns the number
>> > of elements in the array.
>>
>> It looks like Gawk's length(array) extension does not work inside
>> functions. Is this a bug or feature or am I missing something? See the
>> example below. I am using GNU Awk 3.1.6
>>
>> $ cat testdata
>> CD NAME
>> AT Austria
>> BG Bulgaria
>> CH Switzerland
>> DE Germany
>> EE Estonia
>> FR France
>> GR Greece
>>
>> $ cat test.awk
>>
>> # Populate array
>> NR > 1 { array[$1] = $2 }
>>
>> # Print array length and call function A
>> END { print "array:",length(array) ; A(array) }
>>
>> function A(array_A) { print "array_A:", length(array_A) }
>>
>> $ gawk -f test.awk testdata
>> array: 7
>> gawk: test.awk:8: (FILENAME=data FNR=8) fatal: attempt to use array
>> `array_A (from array)' in a scalar context
>>
>> BTW, there is no such error if I have asort(array_A) or asorti(array_A)
>> inside the function.
>>
>> Hermann
>
> I get the same result with gawk 3.1.6 for cygwin. Obviously you can work around
> it since asort() returns the number of elements in an array just like length()
> is supposed to (or "for (i in array) lgth++" if you don't want to be
> gawk-specific) but it does seem like a bug. Anyone know if there's a list of
> known gawk bugs on-line somewhere?
>
> Ed.
>
Thanks for confirming. I wouldn't know of any online Gawk bug list. If
such a thing existed, it would probably mentioned somewhere at:
http://www.gnu.org/software/gawk/ or http://savannah.gnu.org/projects/gawk
The Gawk man page says:
> BUG REPORTS
> If you find a bug in gawk, please send electronic mail to
bug-gawk@gnu.org.
Hermann
|
|
0
|
|
|
|
Reply
|
Hermann
|
3/15/2008 1:50:01 PM
|
|
You cannot pass an array as an argument to a function. However, variables in
awk are in general global, so you can use the actual variable name in the
function.
Rajan
"Hermann Peifer" <peifer@gmx.eu> wrote in message
news:47DBAE29.4050709@gmx.eu...
> Hi All,
>
> The Gawk man page says:
> > Starting with version 3.1.5, as a non-standard extension,
> > with an array argument, length() returns the number
> > of elements in the array.
>
> It looks like Gawk's length(array) extension does not work inside
> functions. Is this a bug or feature or am I missing something? See the
> example below. I am using GNU Awk 3.1.6
>
> $ cat testdata
> CD NAME
> AT Austria
> BG Bulgaria
> CH Switzerland
> DE Germany
> EE Estonia
> FR France
> GR Greece
>
> $ cat test.awk
>
> # Populate array
> NR > 1 { array[$1] = $2 }
>
> # Print array length and call function A
> END { print "array:",length(array) ; A(array) }
>
> function A(array_A) { print "array_A:", length(array_A) }
>
> $ gawk -f test.awk testdata
> array: 7
> gawk: test.awk:8: (FILENAME=data FNR=8) fatal: attempt to use array
> `array_A (from array)' in a scalar context
>
> BTW, there is no such error if I have asort(array_A) or asorti(array_A)
> inside the function.
>
> Hermann
|
|
0
|
|
|
|
Reply
|
Rajan
|
3/23/2008 11:16:10 PM
|
|
On 3/23/2008 6:16 PM, Rajan wrote:
[please don't top-post, fixed below]
>
> "Hermann Peifer" <peifer@gmx.eu> wrote in message
> news:47DBAE29.4050709@gmx.eu...
>
>>Hi All,
>>
>>The Gawk man page says:
>>
>>>Starting with version 3.1.5, as a non-standard extension,
>>>with an array argument, length() returns the number
>>>of elements in the array.
>>
>>It looks like Gawk's length(array) extension does not work inside
>>functions. Is this a bug or feature or am I missing something? See the
>>example below. I am using GNU Awk 3.1.6
>>
>>$ cat testdata
>>CD NAME
>>AT Austria
>>BG Bulgaria
>>CH Switzerland
>>DE Germany
>>EE Estonia
>>FR France
>>GR Greece
>>
>>$ cat test.awk
>>
>># Populate array
>>NR > 1 { array[$1] = $2 }
>>
>># Print array length and call function A
>>END { print "array:",length(array) ; A(array) }
>>
>>function A(array_A) { print "array_A:", length(array_A) }
>>
>>$ gawk -f test.awk testdata
>>array: 7
>>gawk: test.awk:8: (FILENAME=data FNR=8) fatal: attempt to use array
>>`array_A (from array)' in a scalar context
>>
>>BTW, there is no such error if I have asort(array_A) or asorti(array_A)
>>inside the function.
>>
>>Hermann
>
> You cannot pass an array as an argument to a function.
Of course you can.
> However, variables in
> awk are in general global, so you can use the actual variable name in the
> function.
That doesn't help when you're performing the same operation on multiple variables.
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
3/23/2008 11:24:54 PM
|
|
Apologies, Ed is right.
for (i in array_A) len++ and length(array) works length(array_A) doesn't
work in a function. Thanks!
Rajan
"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:47E6E6C6.9040502@lsupcaemnt.com...
>
>
> On 3/23/2008 6:16 PM, Rajan wrote:
>
> [please don't top-post, fixed below]
>
>>
>> "Hermann Peifer" <peifer@gmx.eu> wrote in message
>> news:47DBAE29.4050709@gmx.eu...
>>
>>>Hi All,
>>>
>>>The Gawk man page says:
>>>
>>>>Starting with version 3.1.5, as a non-standard extension,
>>>>with an array argument, length() returns the number
>>>>of elements in the array.
>>>
>>>It looks like Gawk's length(array) extension does not work inside
>>>functions. Is this a bug or feature or am I missing something? See the
>>>example below. I am using GNU Awk 3.1.6
>>>
>>>$ cat testdata
>>>CD NAME
>>>AT Austria
>>>BG Bulgaria
>>>CH Switzerland
>>>DE Germany
>>>EE Estonia
>>>FR France
>>>GR Greece
>>>
>>>$ cat test.awk
>>>
>>># Populate array
>>>NR > 1 { array[$1] = $2 }
>>>
>>># Print array length and call function A
>>>END { print "array:",length(array) ; A(array) }
>>>
>>>function A(array_A) { print "array_A:", length(array_A) }
>>>
>>>$ gawk -f test.awk testdata
>>>array: 7
>>>gawk: test.awk:8: (FILENAME=data FNR=8) fatal: attempt to use array
>>>`array_A (from array)' in a scalar context
>>>
>>>BTW, there is no such error if I have asort(array_A) or asorti(array_A)
>>>inside the function.
>>>
>>>Hermann
>>
>> You cannot pass an array as an argument to a function.
>
> Of course you can.
>
>> However, variables in
>> awk are in general global, so you can use the actual variable name in the
>> function.
>
> That doesn't help when you're performing the same operation on multiple
> variables.
>
> Ed.
>
|
|
0
|
|
|
|
Reply
|
Rajan
|
3/24/2008 12:52:08 AM
|
|
Rajan wrote:
> You cannot pass an array as an argument to a function.
I can. I would even go so far and say: Everybody can.
My bug report has been confirmed by Arnold Robbins and fixed 8 days ago:
Sat Mar 15 22:17:21 2008 Arnold D. Robbins <arnold@skeeve.com>
* builtin.c (do_length): Handle the case of the parameter being
an array that was a function parameter.
http://cvs.savannah.gnu.org/viewvc/gawk-stable/ChangeLog?root=gawk&view=markup
Hermann
|
|
0
|
|
|
|
Reply
|
Hermann
|
3/24/2008 10:08:23 AM
|
|
|
6 Replies
281 Views
(page loaded in 0.073 seconds)
Similiar Articles: How to detect begin and end of a resize operation - comp.lang.tcl ...... Post Question | Groups | Stream ... Hello out there, I need to detect when resizing my toplevel is about to start ... New features added to development gawk - comp.lang.awk... Post Question | ... separate array to keep track of the length of each sub array for ... have used that makes heavy use of gawk's arrays: Say ... newbie: awk not working for multi-byte charsets? - comp.lang.awk ...... wanna know: 1) does awk (I am using gawk ... 2) why this looks so common a question (all ... Unicode" "awk utf" "gawk Unicode" "gawk utf" "awk multibyte string" "awk length ... GAWK: A fix for "missing file is a fatal error" - comp.lang.awk ...In one of my scripts, I found that in GAWK, if a file ... Those are rhetorical questions - I don't really care what ... ARGIND extension gives the ability to detect zero-length ... How to create epoch time variable with gawk? - comp.unix.shell ...The best answer to your question is: man gawk<enter>/mktime<enter> In ... lang.awk How to create epoch time variable with gawk? - comp.unix.shell ... command line length ... AWK question - split string into variables - comp.unix.shell ...... in awk: > * split on ":" character > * check length of array - if not ... How to create epoch time variable with gawk? - comp.unix.shell ... AWK question - split string ... Column width - comp.lang.awkQuick question I'm sure someone can answer easily for me. ... the report, you might be able to load it into an array ... { if (length($1) > max_width} max_width = length($1) x[NR ... Local array variables in functions - comp.lang.awk... Post Question | Groups ... array, local_array) > { > print length(local_array ... gnu.org/viewvc/gawk-stable/ChangeLog?root=gawk ... Count and display the frequency of occurance of each letter in the ...... show at the subject title since last week. the question si ... awk ' { $0 = tolower($0) for(x = 0; x < length($0 ... You can use arbitrary values as the index to awk arrays. the firing squad problem - comp.programming... the firing squad problem: an array of finite state machines ('soldiers'), length N ... > Question: Can a soldier "pulse" to his ... gawk problem matching multiple patterns ... Gawk length(array) question - Application Forum at ObjectMix.comHi All, The Gawk man page says: > Starting with version 3.1.5, as a non-standard extension, > with an array argument, length() returns the Gawk length(array) question - newsgroups.derkeiler.com: The source ...Relevant Pages. Re: Dreamers Wishlist... May be there could be a way how to adress anothers gawk... A member of an array can have subarrays ... #enumerates all ... 7/25/2012 2:24:21 AM
|