| 1 |
# Date: Sat, 15 Mar 2008 16:21:19 +0100 |
| 2 |
# From: Hermann Peifer <[email protected]> |
| 3 |
# Subject: [Fwd: Gawk length(array) bug] |
| 4 |
# To: [email protected] |
| 5 |
# Cc: Aharon Robbins <[email protected]> |
| 6 |
# Message-id: <[email protected]> |
| 7 |
# |
| 8 |
# See below. Regards, Hermann |
| 9 |
# |
| 10 |
# -------- Original Message -------- |
| 11 |
# Subject: Re: Gawk length(array) question |
| 12 |
# Date: Sat, 15 Mar 2008 08:02:03 -0500 |
| 13 |
# From: Ed Morton <[email protected]> |
| 14 |
# Newsgroups: comp.lang.awk |
| 15 |
# References: <[email protected]> |
| 16 |
# |
| 17 |
# On 3/15/2008 6:08 AM, Hermann Peifer wrote: |
| 18 |
# > Hi All, |
| 19 |
# > |
| 20 |
# > The Gawk man page says: |
| 21 |
# > > Starting with version 3.1.5, as a non-standard extension, |
| 22 |
# > > with an array argument, length() returns the number |
| 23 |
# > > of elements in the array. |
| 24 |
# > |
| 25 |
# > It looks like Gawk's length(array) extension does not work inside |
| 26 |
# > functions. Is this a bug or feature or am I missing something? See the |
| 27 |
# > example below. I am using GNU Awk 3.1.6 |
| 28 |
# > |
| 29 |
# > $ cat testdata |
| 30 |
# > CD NAME |
| 31 |
# > AT Austria |
| 32 |
# > BG Bulgaria |
| 33 |
# > CH Switzerland |
| 34 |
# > DE Germany |
| 35 |
# > EE Estonia |
| 36 |
# > FR France |
| 37 |
# > GR Greece |
| 38 |
# > |
| 39 |
# > $ cat test.awk |
| 40 |
# > |
| 41 |
# Populate array |
| 42 |
NR > 1 { array[$1] = $2 } |
| 43 |
|
| 44 |
# Print array length and call function A |
| 45 |
END { print "array:",length(array) ; A(array) } |
| 46 |
|
| 47 |
function A(array_A) { print "array_A:", length(array_A) } |
| 48 |
# > |
| 49 |
# > $ gawk -f test.awk testdata |
| 50 |
# > array: 7 |
| 51 |
# > gawk: test.awk:8: (FILENAME=data FNR=8) fatal: attempt to use array |
| 52 |
# > `array_A (from array)' in a scalar context |
| 53 |
# > |
| 54 |
# > BTW, there is no such error if I have asort(array_A) or asorti(array_A) |
| 55 |
# > inside the function. |
| 56 |
# > |
| 57 |
# > Hermann |
| 58 |
# |
| 59 |
# I get the same result with gawk 3.1.6 for cygwin. Obviously you can work |
| 60 |
# around |
| 61 |
# it since asort() returns the number of elements in an array just like |
| 62 |
# length() |
| 63 |
# is supposed to (or "for (i in array) lgth++" if you don't want to be |
| 64 |
# gawk-specific) but it does seem like a bug. Anyone know if there's a list of |
| 65 |
# known gawk bugs on-line somewhere? |
| 66 |
# |
| 67 |
# Ed. |
| 68 |
# |
| 69 |
# |