accessing list element in loop
Dear Opensesame experts,
After setting a sequence variable in the inline code, such as:
var.set('myvar', 'range(10)')
how do we access the member of the sequence in loop? Defining "[myvar[0]]" in the loop will result in the complaint saying that "variable myvar[0] does not exist". It seems in the loop myvar[0] is treated as a variable name rather than the first member of a list.
Many thanks,
Erik
Comments
Hi Erik,
You cannot access list elements in this way. Different versions of OpenSesame handled this slightly differently, but currently (as of 3.0), OpenSesame simply leaves a string like "[myvar[0]]" untouched, because "myvar[0]" is not recognized as a valid variable name, and OpenSesame script doesn't support indexing.
What you could do is use Python in the loop table, like so:
See also:
Also, in your example you're not defining a list, but the string 'range(10)'. What you want to do is:
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
Thanks for your comments. It's good to know that Opensesame script doesn't support indexing. I will try out the python syntax in the loop.
Best,
Erik
Hi Sebastiaan,
Defining python code in the loop table to access predefined var.myvar in inline script brings up the following error:
Failed to evaluate 'var.myvar[0]' in loop item 'new_loop': 'unicode' object has no attribute 'myvar'
So do I simply type
in the loop table, or there are some other tricks?
Best,
Erik