Your message dated Fri, 25 Apr 2014 09:03:19 +0200
with message-id <[email protected]>
and subject line Re: Bug#725426: gawk(1) man page is unclear regarding for 
loops with multi-index arrays
has caused the Debian Bug report #725426,
regarding gawk(1) man page is unclear regarding for loops with multi-index 
arrays
to be marked as done.

This means that you claim that the problem has been dealt with.
If this is not the case it is now your responsibility to reopen the
Bug report if necessary, and/or fix the problem forthwith.

(NB: If you are a system administrator and have no idea what this
message is talking about, this may indicate a serious mail system
misconfiguration somewhere. Please contact [email protected]
immediately.)


-- 
725426: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=725426
Debian Bug Tracking System
Contact [email protected] with problems
--- Begin Message ---
Package: gawk
Version: 1:4.0.1+dfsg-2.1
Severity: normal

Hello,
thanks for maintaining gawk in Debian!

I noticed that the gawk(1) man page is somewhat misleading regarding
for loops that involve multi-index arrays.

The man page says (in section "Arrays"):

[...]
| The  special  operator  in may be used to test if an array has an index
| consisting of a particular value:
|
|        if (val in array)
|             print array[val]
|
| If the array has multiple subscripts, use (i, j) in array.
|
| The in construct may also be used in a for loop to iterate over all the
| elements of an array.
[...]

This made me think that I could write:

  for ((i, j) in array)
         print array[i, j]

Unfortunately, this does not work!
It seems that the other three possibilities work correctly:

  $ gawk 'BEGIN { array[42] = "answer"; if (42 in array) print array[42]; }'
  answer
  $ gawk 'BEGIN { array[42] = "answer"; for (val in array) print array[val]; }'
  answer
  $ gawk 'BEGIN { array[4,2] = "answer"; if ((4,2) in array) print array[4,2]; 
}'
  answer

but iterating on multi-index arrays does not:

  $ gawk 'BEGIN { array[4,2] = "answer"; for ((i,j) in array) print array[i,j]; 
}'
  gawk: cmd. line:1: BEGIN { array[4,2] = "answer"; for ((i,j) in array) print 
array[i,j]; }
  gawk: cmd. line:1:                                                   ^ syntax 
error

The man page does not clearly explain this.

I found confirmation on this page:
http://stackoverflow.com/questions/14280877/multidimensional-arrays-in-awk
Please note that the reply also suggests a workaround for the missing
feature.
Is there any simpler or more elegant (but still portable to other AWK
implementations, such as mawk) way to iterate on multi-index arrays?

Please enhance the man page by clearly describing this unexpected
behavior and (possibly) by suggesting a workaround.
Please also forward this bug report upstream, if appropriate.

Thanks for your time!


-- System Information:
Debian Release: jessie/sid
  APT prefers testing
  APT policy: (800, 'testing'), (500, 'unstable')
Architecture: amd64 (x86_64)

Kernel: Linux 3.10-3-amd64 (SMP w/2 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages gawk depends on:
ii  libc6         2.17-93
ii  libreadline6  6.2+dfsg-0.1
ii  libsigsegv2   2.10-2

gawk recommends no packages.

Versions of packages gawk suggests:
pn  gawk-doc  <none>

-- no debconf information

--- End Message ---
--- Begin Message ---
Version: 1:4.1.1+dfsg-1


Hello,

Thanks for your bug report. I agree that the man page of gawk was
unclear about for loops with multi-index arrays. Fortunately this has
been clarified in the latest version:

| The in construct may also be used in a for loop to iterate over all
| the elements  of  an  array. However, the (i, j) in array construct
| only works in tests, not in for loops.

The 'workaround' mentioned on the linked StackOverflow page is indeed
the best way to iterate over multi-index arrays in Awk.

The workaround is also documented in the Gawk manual [1], also
available on Debian in the gawk-doc package. (Note that Gawk also has
proper 'arrays of arrays' [2], but this is not portable to other Awk
implementations).

[1]: https://www.gnu.org/software/gawk/manual/html_node/Multi_002dscanning.html
[2]: https://www.gnu.org/software/gawk/manual/html_node/Arrays-of-Arrays.html

Regards,

Jeroen Schot

--- End Message ---

Reply via email to