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


-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to