manpagez: man pages & more
info gawk
Home | html | info | man

File: gawk.info,  Node: Multiscanning,  Up: Multidimensional

8.5.1 Scanning Multidimensional Arrays
--------------------------------------

There is no special 'for' statement for scanning a "multidimensional"
array.  There cannot be one, because, in truth, 'awk' does not have
multidimensional arrays or elements--there is only a multidimensional
_way of accessing_ an array.

   However, if your program has an array that is always accessed as
multidimensional, you can get the effect of scanning it by combining the
scanning 'for' statement (*note Scanning an Array::) with the built-in
'split()' function (*note String Functions::).  It works in the
following manner:

     for (combined in array) {
         split(combined, separate, SUBSEP)
         ...
     }

This sets the variable 'combined' to each concatenated combined index in
the array, and splits it into the individual indices by breaking it
apart where the value of 'SUBSEP' appears.  The individual indices then
become the elements of the array 'separate'.

   Thus, if a value is previously stored in 'array[1, "foo"]', then an
element with index '"1\034foo"' exists in 'array'.  (Recall that the
default value of 'SUBSEP' is the character with code 034.)  Sooner or
later, the 'for' statement finds that index and does an iteration with
the variable 'combined' set to '"1\034foo"'.  Then the 'split()'
function is called as follows:

     split("1\034foo", separate, "\034")

The result is to set 'separate[1]' to '"1"' and 'separate[2]' to
'"foo"'.  Presto!  The original sequence of separate indices is
recovered.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.