GNU Info

Info Node: (gawk.info)Multi-scanning

(gawk.info)Multi-scanning


Next: Array Sorting Prev: Multi-dimensional Up: Arrays
Enter node , (file) or (file)node

Scanning Multidimensional Arrays
================================

   There is no special `for' statement for scanning a
"multidimensional" array. There cannot be one, because in truth there
are no 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 All Elements of an Array:
Scanning an Array.) with the built-in `split' function (Note: String
Manipulation 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.


automatically generated by info2www version 1.2.2.9