Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

accessing list element in loop

edited June 2016 in OpenSesame

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

  • edited 9:48AM

    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:

    =var.myvar[0]
    

    See also:

    Also, in your example you're not defining a list, but the string 'range(10)'. What you want to do is:

    var.myvar = range(10)
    

    Cheers!
    Sebastiaan

  • edited 9:48AM

    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

  • edited 9:48AM

    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

    =var.myvar[0]
    

    in the loop table, or there are some other tricks?

    Best,

    Erik

Sign In or Register to comment.