I have an awk script that I am writing and I needed to make use of a multidimensional array to hold some data... Which is all fine but I need to loop through that array now and I have no idea how to do that.
for a regular array, the following works:
ARRAY[NUMBER]
for(var in ARRAY) {
...
}
but I get syntax errors if I try the following:
ARRAY[WORD][NUMBER]
for(var in ARRAY[1]) {
...
}
syntax error at or near [
How can I loop through a multidimensional array?
---------- Post updated at 09:37 AM ---------- Previous update was at 09:31 AM ----------
Upon further thinking I just created a new variable to mark how many elements were in the second index in the array. If there is a way to loop through the array without having to do that I would like to know.
Yes, as already stated, with your version you can simulate multidimensional arrays. If you need a somehow more granular access to the elements,
you can use something like this:
awk 'BEGIN {
for (x = 0; ++x <= 10;)
for (y = 0; ++y <= 5;)
aOa[x, y] = "element:" FS x FS y
for (i in aOa) {
split(i, t, SUBSEP)
print aOa[t[1], t[2]]
}
}'
Otherwise, you can download and install GNU awk 4.