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

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

Re: Gawk length(array) question


From: Aharon Robbins
Subject: Re: Gawk length(array) question
Date: Sat, 15 Mar 2008 22:27:16 +0200

Hermann, thanks for forwarding this to the bug address. That is the
proper way to report bugs.

There isn't a "known bugs list" as I try to fix all the bugs that
I know about.  I have one or two pending in my inbox that I haven't
decided yet if they really are bugs, or if I really want to fix them,
but in general, when I know about something, I try to fix it.

To sum up, this is a bug, plain and simple.  The patch is below, and it
will be making its way into the CVS archive shortly.

Thanks for finding and reporting this.

Arnold

In article <address@hidden>, Hermann Peifer  <address@hidden> wrote:
>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 
> > address@hidden
>
>Hermann

------------------ cut here ------------------
Sat Mar 15 22:17:21 2008  Arnold D. Robbins  <address@hidden>

        * builtin.c (do_length): Handle the case of the parameter being
        an array that was a function parameter.

Index: builtin.c
===================================================================
RCS file: /d/mongo/cvsrep/gawk-stable/builtin.c,v
retrieving revision 1.23
diff -u -r1.23 builtin.c
--- builtin.c   11 Mar 2008 20:50:16 -0000      1.23
+++ builtin.c   15 Mar 2008 20:16:25 -0000
@@ -436,10 +436,15 @@
 {
        NODE *tmp;
        size_t len;
+       NODE *n;
 
-       if (tree->lnode->type == Node_var_array
-           || tree->lnode->type == Node_array_ref) {
-               NODE *array_var = tree->lnode;
+       n = tree->lnode;
+       if (n->type == Node_param_list)
+               n = stack_ptr[n->param_cnt];
+
+       if (n->type == Node_var_array
+           || n->type == Node_array_ref) {
+               NODE *array_var = n;
                static short warned = FALSE;
 
                if (array_var->type == Node_array_ref)
@@ -455,7 +460,7 @@
                return tmp_number((AWKNUM) array_var->table_size);
        } else {
 normal:
-               tmp = tree_eval(tree->lnode);
+               tmp = tree_eval(n);
                if (do_lint && (tmp->flags & (STRING|STRCUR)) == 0)
                        lintwarn(_("length: received non-string argument"));
                tmp = force_string(tmp);
-- 
Aharon (Arnold) Robbins                                 arnold AT skeeve DOT com
P.O. Box 354            Home Phone: +972  8 979-0381    Fax: +1 206 202 4333
Nof Ayalon              Cell Phone: +972 50  729-7545
D.N. Shimshon 99785     ISRAEL




reply via email to

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