I am trying to grab a list of files through the Pick File() function.
I want to loop through the list of files and for each file I want to perform a stack, calculate some statistics from a column in the stacked data table, then use the calculated values to update an existing data table.
(The list of files is a list of production lots and I am trying to create a script that trends the mean, stdev, and CPK of the production lots)
When I only look at one file I can get the above steps to work but as soon as I try multiple files I cannot seem to get it to work. The for loop looping through the files only runs once. I have placed a show(i==NItems(lst)) statement and it even shows that the condition for the loop is not met.
Below is an excerpt with the for loop in question.
get_lst_loop = Function({files},
Local({tmp,lst,dt,dt_stack},
Try(
tmp = {};
For(i=1,i<=NItems(files),i++,
show(i==NItems(files));
dt = Open(files[i],Invisible);
dt_stack = bRay_stack(dt);
lst = get_lst(dt_stack,"Data");
Close(dt_stack,NoSave);
Close(dt,NoSave);
Insert Into(tmp,lst)
);
,
Throw("Error in making analysis matrix: "||char(exception_msg));
);
Return(Eval List(tmp));
);
);I will attach the rest of the script and a few testing files to this post.
I have tried a number of different things to get around this but nothing seems to be working.
If there is a better way to accomplish what I am trying please let me know.
Or you could just make i also a local variable.
Whilst it is possible (and sometimes convenient) to explicitly define the local variables associated with a function definition, I think in general it us much more preferable to treat all variables as local to the function definition. This way you wont get into the situation where your for-loop variable is accidentally global. You can declare all variables local using the following syntax:
foo = function( {}, {Default Local},
// implement function here e.g.
for (i=1,i<=10,i++,
// do something
);
return(somevalue);
);Just in case I have figured out where the script was going wrong. Maybe someone can elaborate more on why this occurs.
The error seemed to be due to scope issues with my incrementing variable in my for loops.
I was using multiple for loops that were all using the same "i" as the incrementing variable. By changing it so each loop had a unique incrementing variable (like "fi", "lst_i")