how do i create two nested loops?Hi - I am attempting to write a script that follows this workflow:
1 - begin a loop on a certain starting datetime (e.g. Oct 1, 2021 11:00 AM). I'll refer to this as row n. Obtain value from col1[row n].
2 - go to next row, n+1. set col2 = col1 [n+1] - col1[n].
3 - go to next row, n+2, set col2 = col1 [n+2] - col1[n]
4 - go to next row, n+3, set col2 = col1[ n+3] - col1[n]. Continue to loop to through each subsequent row until datetime[n+k] = datetime[n] + 7 days. (Note that k will vary depending on the time increment between rows as the loop proceeds; e.g. the time increases from one row to the next by 5 mins, 15 mins, 1 hr, etc, depending on the row in question.)
5 - Repeat steps 1 to 4 for each new week of data by using a new "start date" obtained in step 4. Continue looping through another 7 days of data.
6 - Repeat step 5 until data table runs out of rows.
The goal is to do computations on col1 data that compare a week's worth of rows to the first row of that week, and then to repeat this over and over for each subsequent week (week 2, week 3, week 4, etc) until all rows have been processed. : )
ScriptingRe: how do i create two nested loops?Can you give a small data table with what you expect the data to be? Or at least what the data table you're using looks like? It sounds like a column formula would be a simpler solution.
Re: how do i create two nested loops?I think you might be saying something like this?
Names default to here(1);
dt = New Table( "Untitled 1133",
Add Rows( 5 ),
New Column( "Week1",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 22, 0 ),
Input Format( "d/m/y h:m:s", 0 ),
Set Values( [3716957885, 3716957886, 3716957887, 3716957888, 3716957889] )
),
New Column( "Week2",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 22, 0 ),
Input Format( "d/m/y h:m:s", 0 ),
Set Values( [3716957885, 3716957886, 3716957887, 3716957888, 3716957889] )
),
New Column( "Week3",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 22, 0 ),
Input Format( "d/m/y h:m:s", 0 ),
Set Values( [3716957885, 3716957886, 3716957887, 3716957888, 3716957889] )
)
);
nc = ncols(dt);
for(i=1, i<=nc, i++,
col_name = column(i) << Get Name(); // get the original column name
Eval(EvalExpr( // you have to do this so you can substitute in the actual col_name instead of a variable
dt << New Column(col_name || " comparison",
// just take current row minus first row
formula(Expr(column(dt, col_name))[row()]-Expr(column(dt, col_name))[1])
)
));
);
Re: how do i create two nested loops?Sure. Thank you. Attached is a snippet of a dataset that I created in excel as an example.