Multidimensional Array Iteration

DCurrent

Site Owner, OpenBOR Project Leader
Staff member
Hey guys, while working on a new afterimage library I needed to whip up an iterator for literal keyed multidimensional arrays. Figured I'd post a quick and dirty example here of how to do it. This example is of a 2D array, but if you're crazy enough you can use the logic to add dimensions to infinity. Or at least until your platform hacks up a RAM chip. :)

This is what the array looks like as a table (since that's exactly what it is).

. ABCDE
0 ABCDE
1 ZYXWV

C:
void array_cols_a;
            void array_cols_b;
            void array_rows;
            void column_array;
            void column_element;
            int  column_last;
            int  row_last;

            // Column values. We need to create the array of
            // columns first, since they will be inserted
            // into a row array element.
            array_cols_a = array(5);
            set(array_cols_a, "A", "A");
            set(array_cols_a, "B", "B");
            set(array_cols_a, "C", "C");
            set(array_cols_a, "D", "D");
            set(array_cols_a, "E", "E");

            array_cols_b = array(5);
            set(array_cols_b, "A", "Z");
            set(array_cols_b, "B", "Y");
            set(array_cols_b, "C", "X");
            set(array_cols_b, "D", "W");
            set(array_cols_b, "E", "V");

            // Rows array. Each row element is an array of columns.
            // Create array.
            array_rows = array(2);
            set(array_rows, "0", array_cols_a);
            set(array_rows, "1", array_cols_b);

            // Reset row cursor.
            reset(array_rows);

            // Loop all elements of row array.
            do
            {
                // Is this the last row?
                row_last = islast(array_rows);

                log("Row - key: ");
                log(key(array_rows));
                log("\n");

                // Get current row value, which
                // will be an array of columns.
                column_array = value(array_rows);

                // Move to cursor to next array
                // element and get cursor value.
                // Will be 0 if we're already
                // at last array element.
                next(array_rows);


                // Reset cursor for our current
                // array of columns.
                reset(column_array);

                // Loop all elements of column array.
                do
                {
                    column_last = islast(column_array);

                    log("Column - Key: ");
                    log(key(column_array));

                    column_element = value(column_array);
                    next(column_array);

                    log(", value: " + column_element);
                    log("\n");

                }while(!column_last)

                log("\n\n");
            }
            while(!row_last);

Run this in a function, and you'll get the following log output:

Code:
Row - key: 0
Column - Key: A, value: A
Column - Key: B, value: B
Column - Key: C, value: C
Column - Key: D, value: D
Column - Key: E, value: E
Row - key: 1
Column - Key: A, value: Z
Column - Key: B, value: Y
Column - Key: C, value: X
Column - Key: D, value: W
Column - Key: E, value: V

Enjoy!

DC
 
Very nice one, DC. I also wasn't aware of value and reset. When I first looked this thread, I thought it was about 3D parallax for stages like Street Fighter 2 and some racing games especially F-Zero, F-1 Race, and Rad Racer. LOL
 
Back
Top Bottom