Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Gawk length(array) question
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

Report this thread to moderator Post Follow-up to this message
Old Post
Hermann Peifer
03-15-08 12:58 PM


Re: Gawk length(array) question

On 3/15/2008 6:08 AM, Hermann Peifer wrote:
> Hi All,
>
> The Gawk man page says: 
>
> 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 aro
und
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-16-08 03:06 AM


Re: Gawk length(array) question
Ed Morton wrote:
>
> On 3/15/2008 6:08 AM, Hermann Peifer wrote: 
>
> I get the same result with gawk 3.1.6 for cygwin. Obviously you can work a
round
> it since asort() returns the number of elements in an array just like leng
th()
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Hermann Peifer
03-16-08 03:06 AM


Re: Gawk length(array) question
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: 
>
> 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


Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-24-08 03:00 AM


Re: Gawk length(array) question

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...
> 
>
> 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 vari
ables.

Ed.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-24-08 03:00 AM


Re: Gawk length(array) question
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]
> 
>
> Of course you can.
> 
>
> That doesn't help when you're performing the same operation on multiple
> variables.
>
> Ed.
>

Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-24-08 03:00 AM


Re: Gawk length(array) question
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.


[url]http://cvs.savannah.gnu.org/viewvc/gawk-stable/ChangeLog?root=gawk&view=markup[/ur
l]

Hermann

Report this thread to moderator Post Follow-up to this message
Old Post
Hermann Peifer
03-24-08 12:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:44 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.