# Nested loop question

**URL:** <https://mworks.discourse.group/t/nested-loop-question/318>\
**Category:** Support\
**Created:** [July 6, 2018, 2:29pm UTC](https://mworks.discourse.group/t/nested-loop-question/318 "2018-07-06T14:29:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 6, 2018, 2:29pm UTC](https://mworks.discourse.group/t/nested-loop-question/318/1 "2018-07-06T14:29:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 6, 2018, 3:15pm UTC](https://mworks.discourse.group/t/nested-loop-question/318/2 "2018-07-06T15:15:14Z")

</div>

Hi Sujay,

As we discussed offline, the way to do this is to have one [range replicator](https://mworks.github.io/documentation/latest/components/range_replicator.html) 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](https://mworks.github.io/documentation/latest/components/task_system.html):

```
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

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 19, 2022, 9:44pm UTC](https://mworks.discourse.group/t/nested-loop-question/318/3 "2022-07-19T21:44:20Z")

</div>


