Reputation: 67507
I don't have access to other awk
versions to check the generality of this issue.
$ awk --version
GNU Awk 3.1.5
consider this simple test
$ awk 'function f(x) {x[1]} BEGIN{f(x); print length(x)}'
1
works as expected, however printing the length in the function body fails
$ awk 'function f(x) {x[1]; print length(x)} BEGIN{f(x)}'
awk: fatal: attempt to use array `x (from x)' in a scalar context
the only way to get the length of an array in a function body seems to be counting the elements for(i in x) c++
this is inspired by this answer
Upvotes: 2
Views: 610
Reputation: 203995
Once upon a time length()
was for finding the number of characters in a string. Then gawk modified it to also find the number of elements in an array. Then POSIX adopted the gawk approach and here we are today with all awks cheerfully taking strings or arrays as arguments to length().
What you're experiencing doesn't happen in modern awk versions, it was a bug in gawk 3.1.5 and 3.1.6, see https://lists.gnu.org/archive/html/bug-gnu-utils/2008-03/msg00028.html
Upvotes: 4