Next: , Previous: Multi-dimensional, Up: Arrays



7.10 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 (see Scanning an Array) with the built-in split function (see 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.