Nested loop question

Hi Chris,

I am a bit confused how list can be used to make something like a for loop.

In this example, how could I create a loop inside the existing loop with the same list as for_each_stim? I am not sure how the list {…} and for_each_stim{…} are related here.

var stim_index (scope = local; default_value = 0)

%define for_each_stim ()
range_replicator (
variable = stim_index
from = 0
to = num_stims - 1
step = 1
)
%end

protocol ‘Mental Time Travel 2_images’ {
start_io_device (keyboard)

block (nsamples = num_trials_per_stim) {

list (
selection = random_without_replacement
nsamples = num_stims
sampling_method = samples
) {
for_each_stim {
trial {
task {
state ‘Begin trial’ {
target_stim_index = stim_index


Thanks,


Sujay

Hi Sujay,

As we discussed offline, the way to do this is to have one range replicator inside another, with a separate index variable for each:

list (selection = random_without_replacement) {
    range_replicator (
        variable = outer_index
        from = 0
        to = num_stims - 1
        step = 1) {
        range_replicator (
            variable = inner_index
            from = 0
            to = num_stims - 1
            step = 1) {
            trial {
                ...
            }
        }
    }
}

To omit the case where outer_index and inner_index are the same, include a test for this in the first state of your task:

task {
    state 'Check for duplicate stim' {
        goto (
            target = 'Skip duplicate stim'
            when = outer_index == inner_index
            )
        goto ('Begin trial')
    }
    ...
    state 'Skip duplicate stim' {
        yield ()
    }
}

(Sorry, I couldn’t think of a more elegant way to do this.)

Cheers,
Chris