Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Gawk length...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 7 Topic 2191 of 2236
Post > Topic >>

Re: Gawk length(array) question

by "Rajan" <svrajan@[EMAIL PROTECTED] > Mar 23, 2008 at 08:52 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@[EMAIL PROTECTED]
> wrote in message 
news:47E6E6C6.9040502@[EMAIL PROTECTED]
>
>
> On 3/23/2008 6:16 PM, Rajan wrote:
>
> [please don't top-post, fixed below]
>
>>
>> "Hermann Peifer" <peifer@[EMAIL PROTECTED]
> wrote in message
>> news:47DBAE29.4050709@[EMAIL PROTECTED]
>>
>>>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.
>




 7 Posts in Topic:
Gawk length(array) question
Hermann Peifer <peifer  2008-03-15 12:08:25 
Re: Gawk length(array) question
Ed Morton <morton@[EMA  2008-03-15 08:02:03 
Re: Gawk length(array) question
Hermann Peifer <peifer  2008-03-15 14:50:01 
Re: Gawk length(array) question
"Rajan" <svr  2008-03-23 19:16:10 
Re: Gawk length(array) question
Ed Morton <morton@[EMA  2008-03-23 18:24:54 
Re: Gawk length(array) question
"Rajan" <svr  2008-03-23 20:52:08 
Re: Gawk length(array) question
Hermann Peifer <peifer  2008-03-24 11:08:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 0:30:31 CDT 2008.