[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Sample code in Gawk manual section 9.2.5
From: |
Aharon Robbins |
Subject: |
Re: [bug-gawk] Sample code in Gawk manual section 9.2.5 |
Date: |
Sun, 27 Jan 2013 08:58:23 +0200 |
User-agent: |
Heirloom mailx 12.5 6/20/10 |
Hi Hermann,
> Date: Sat, 26 Jan 2013 19:53:36 -0200
> From: Hermann Peifer <address@hidden>
> To: address@hidden
> Subject: [bug-gawk] Sample code in Gawk manual section 9.2.5
>
> Hi,
>
> As far as I can see, the sample code does not work at all with modern
> gawk, as I end up with this error:
>
> awk: test.awk:3: fatal: attempt to use scalar parameter `a' as an array
>
> > cat test.awk
> function foo(a)
> {
> a[1] = 1 # parameter is an array
> }
>
> BEGIN {
> b=1
> foo(b) # invalid: fatal type mismatch
> foo(x) # x uninitialized, becomes an array dynamically
> x = 1 # now not allowed, runtime error
> }
>
>
> > awk -V | head -1
> GNU Awk 4.0.72 (GNU MPFR 3.1.1-p2, GNU MP 5.0.5)
>
> I noted that my MacBook's /usr/bin/awk behaves differently:
>
> > /usr/bin/awk --version
> awk version 20070501
>
> > /usr/bin/awk -f test.awk
> /usr/bin/awk: can't assign to x; it's an array name.
> source line number 10
>
> Hermann
The MacBook's awk is an old version of Brian Kernighan's awk, which
still displays the bug (it's a bug in his awk, not in gawk).
Here are the results I get:
$ gawk '{ print NR, $0 }' /tmp/t1.awk
1 function foo(a)
2 {
3 a[1] = 1 # parameter is an array
4 }
5
6 BEGIN {
7 b=1
8 foo(b) # invalid: fatal type mismatch
9 foo(x) # x uninitialized, becomes an array dynamically
10 x = 1 # now not allowed, runtime error
11 }
$ for i in nawk mawk gawk mksawk
> do echo ========== $i
> $i -f /tmp/t1.awk
> done
========== nawk
nawk: can't assign to x; it's an array name.
source line number 10
========== mawk
mawk: /tmp/t1.awk: line 8: type error in arg(1) in call to foo
mawk: /tmp/t1.awk: line 10: illegal reference to array x
========== gawk
gawk: /tmp/t1.awk:3: fatal: attempt to use scalar parameter `a' as an array
========== mksawk
mksawk: line 3 (B): scalar "a" cannot be used as array
In short, Brian's awk is wrong here. (I have bcc'ed him).
I just now noticed that your report relates to the manual. Are you
saying that because the first call is a fatal error gawk never reports
the other error? That's true. This could perhaps be more clear in
the doc, and I've just added another paragraph.
Thanks for the resport,
Arnold