What's new

Shortcutting Nested Loops

Hi,

I have this loop:

Code:
          for i:= 0 to 31
                        step_bars_0[i] := preset[418 + i]
                        step_bars_1[i] := preset[418 + 32 + i]
                        step_bars_2[i] := preset[418 + 32 * 2 + i]
                        step_bars_3[i] := preset[418 + 32 * 3 + i]
                        step_bars_4[i] := preset[418 + 32 * 4 + i]
                        step_bars_5[i] := preset[418 + 32 * 5 + i]
                        step_bars_6[i] := preset[418 + 32 * 6 + i]
                        step_bars_7[i] := preset[418 + 32 * 7 + i]
                        step_bars_8[i] := preset[418 + 32 * 8 + i]
                        step_bars_9[i] := preset[418 + 32 * 9 + i]
                        step_bars_10[i] := preset[418 + 32 * 10 + i]
                        step_bars_11[i] := preset[418 + 32 * 11 + i]
                        step_bars_12[i] := preset[418 + 32 * 12 + i]
                        step_bars_13[i] := preset[418 + 32 * 13 + i]
                        step_bars_14[i] := preset[418 + 32 * 14 + i]
                        step_bars_15[i] := preset[418 + 32 * 15 + i]
                    end for

And I'm trying to shortcut it to:
Code:
                    for i:= 0 to NUM_SLOTS - 1
                        for j:= 0 to 31
                            step_bars_[i][j] := preset[418 + 32 * i + j]
                        end for
                    end for


But there's a syntax error because of the two consecutive [ ][ ]...
I guess I should do an iterative macro somehow but I can't figure the syntax out...

Any help would be greatly appreciated!
HD
 
Instead of [][] you need to use [i, j], no need for consecutive macros.

Ideally your preset array should also be multidimensional so that you don't need to bother yourself with those offsets...
 
Let's see if I get this straight - you mean that I should turn step_bars_ into one multidimensional array instead of using 16 different step_bar_ variables?
 
Yes! Especially if you don't intend to change things for them, as in increasing the number of steps or increasing their instance count (in your case it's 16 now).
 
The only problem now is that the individual 16 "step_bars_" variables are actually ui tables and I have to keep them as individual tables.

They are declared as:
Code:
declare read ui_table step_bars_[NUM_SLOTS][32] (2,2,127)

That's why I needed something like this:
Code:
                    for i:= 0 to NUM_SLOTS - 1
                        for j:= 0 to 31
                            step_bars_[i][j] := preset[418 + 32 * i + j]
                        end for
                    end for

The "i" loop for the name and the "j" loop for the table step value.
 
So actually you can use get_control_par_arr() to get the values of ui_tables in a loop. So you can scan all 16 of your ui_tables in one nested loop and get/set values like that!

(Your preset array should still be multidimensional, for simpler code!)
 
You can not use a looping variable inside a variable name like that, because variable names are something that only exist in our eyes, they don't exist as something evaluated by the computer when the program is actually running.

You can use "iterate_macro()" in SublimeKSP to loop through your tables (basically you only write one line, but SublimeKSP will automatically expand it to the correct 16 lines behind the scenes).

Or, like Mario said, you can use your "i" variable loop through the control id's in that control array, calling the get_control_par_arr().
 
In a nutshell:

Code:
on init
    define NUM_TABLES := 16
    define NUM_TABLE_STEPS := 32

    declare i
    declare j
    declare pers table_preset[NUM_TABLES, NUM_TABLE_STEPS]

    declare ui_table step_bars_[NUM_TABLES][NUM_TABLE_STEPS] (2, 2, 127)

   { storing ui_table values into a preset array }
    for i := 0 to NUM_TABLES - 1
        for j := 0 to NUM_TABLE_STEPS - 1
            table_preset[i, j] := get_control_par_arr(step_bars_[i], CONTROL_PAR_VALUE, j)
        end for
    end for

    { readning the preset array data and applying it to the ui_table values }
    for i := 0 to NUM_TABLES - 1
        for j := 0 to NUM_TABLE_STEPS - 1
            set_control_par_arr(step_bars_[i], CONTROL_PAR_VALUE, table_preset[i, j], j)
        end for
    end for
end on
 
Top Bottom