www.delorie.com/gnu/docs/gawk/gawk_129.html   search  
 
Buy the book!


The GNU Awk User's Guide

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.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 section Scanning All Elements of an Array) with the built-in split function (see section 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.


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003