# Problem controlling a loop using "selected rows" with a variable that is defined in a previous routine (ONLINE)

**URL:** <https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040>\
**Category:** Online experiments\
**Created:** [August 28, 2020, 3:53pm UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040 "2020-08-28T15:53:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_bsms](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@harry\_bsms](https://discourse.psychopy.org/u/harry_bsms)\
**Post date:** [August 28, 2020, 3:53pm UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040/1 "2020-08-28T15:53:21Z")

</div>

URL of experiment:  
[https://gitlab.pavlovia.org/harry\_witchel/mw-28-8-20-4-07pm/blob/master/html/goNoGo\_2020-08-26\_mental\_effort.js](https://gitlab.pavlovia.org/harry_witchel/mw-28-8-20-4-07pm/blob/master/html/goNoGo_2020-08-26_mental_effort.js)

Description of the problem:

Hi all,

My experiment works perfectly well when running it locally via python.

I am trying to control a loop (“block”) in psychopy builder with a variable that is defined in a code component in “plan1” (the previous routine), which is outside the “block” loop - see below (rowsFromLearningBlock):

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/8/88aaef9dbfd75a1c778440f3ce21d3aad794f14d.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/3/a35d945bf52fb7a859ed99230ee857b0cb569c78.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/6/e/6ec7c5833bd40077626672d7a67a6a653893f98b.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/3/a359ceb9b333644e83036c924aa1ebc3ab7df6d7.png)

I have tried to use the window object (“window.rowsFromLearningBlock”) in order to make the variable global, but this hasn’t worked either. I have tried without the $ sign, but this also doesn’t work. The only solution I can see is duplicating the routines for each trial and discarding the current loops, in order to have a new conditions file with the correct rows defined for each one. However, I would rather not do this as there is a lot of trials.

Thank you for any help.

---

<div class="post-metadata">

**Author:** ![kimSpeck](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimspeck/32/10151_2.png) [@kimSpeck](https://discourse.psychopy.org/u/kimSpeck)\
**Post date:** [August 28, 2020, 4:17pm UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040/2 "2020-08-28T16:17:18Z")

</div>

Given your error message I wouldn’t assume a scope issue here.

I think the input for Selected rows in the loop has to be a string.  
Given the screenshot of your code I don’t really know whether that’s the case.

You can maybe try something like this:

```auto
startB = startRowBlock.toString();
endB = endRowBlock.toString();
CondRows = startB + ":" + endB;

```

with CondRows as variable to enter in Selected rows.

---

<div class="post-metadata">

**Author:** ![harry\_bsms](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@harry\_bsms](https://discourse.psychopy.org/u/harry_bsms)\
**Post date:** [August 29, 2020, 9:45am UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040/3 "2020-08-29T09:45:00Z")

</div>

Hi, thank you for the reply.

I just tried this and got exactly the same error as the one I originally posted. It seems like the array still isn’t being recognised as valid. I’ve included a screenshot of the conditions file if it helps.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/b/6/b6f1ed558618d3fa20a2530df04b733d50b2c6e8.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/6/7/672e5ba5d455ae2d862dbf1bf4840180874a24d0.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/8/c89585a22a8912ca0613f93c0d9686e97b1c5ecd.png)

After the error occurs, doing console.log(rowsFromLearningBlock) returns “0:0” - is this where it is becoming invalid length? The intention is just to select the first row of the conditions file because later on, I want to select multiple rows.

I have tested inputting “0:0” in to the selected rows box and I get the same invalid length error, whereas I get no error when inputting “0:1”. I believe that this may be the source. Is there an alternative to just select the first row using this code?

Thanks again.

> **[Sign in](https://gitlab.pavlovia.org/users/sign_in)**
>
> Sign In/Register

---

<div class="post-metadata">

**Author:** ![kimSpeck](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimspeck/32/10151_2.png) [@kimSpeck](https://discourse.psychopy.org/u/kimSpeck)\
**Post date:** [August 31, 2020, 1:08pm UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040/4 "2020-08-31T13:08:27Z")

</div>

Yep.  
Since the last number in “0:0” or “0:1” is not included in the generated sequence of numbers you basically select no row in case of “0:0” (0:1 evalualtes to 0).

I assume if you entered for example 9 in the rowBlockEnd column of your condition file the 9th row should be included as well?  
If that’s the case you could either change your condition file by adding 1 to every entry in the rowBlockEnd column or add 1 to the variable endB.

---

<div class="post-metadata">

**Author:** ![harry\_bsms](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@harry\_bsms](https://discourse.psychopy.org/u/harry_bsms)\
**Post date:** [August 31, 2020, 1:37pm UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040/5 "2020-08-31T13:37:55Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![Joanne](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@Joanne](https://discourse.psychopy.org/u/Joanne)\
**Post date:** [March 3, 2025, 11:39pm UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040/6 "2025-03-03T23:39:32Z")

</div>

This is really helpful. Variables for row selection need to be strings not integers!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 4, 2025, 3:52am UTC](https://discourse.psychopy.org/t/problem-controlling-a-loop-using-selected-rows-with-a-variable-that-is-defined-in-a-previous-routine-online/16040/7 "2025-03-04T03:52:12Z")

</div>

Strings of the form x:y or lists of integers.

> [@Wakefield's Daily Tips](https://discourse.psychopy.org/t/wakefields-daily-tips/43371/23):
>
> Avoid multiple spreadsheets One way of counterbalancing blocks in an experiment is to have an outer loop which points to a spreadsheet which contains the names of different spreadsheets to use in an inner loop. The main issue with this is that if you have multiple spreadsheets with the same columns and you want to make a change, you have to make it to all of them. The other more minor issue is that you may have to specify the spreadsheets as additional resources in Experiment Settings / Online. …
