Millisecond Forums

Substring function incorrectly defined in language reference?

https://forums.millisecond.com/Topic34742.aspx

By Sercan - 9/20/2022

I am trying to pull a single character at index values.trialcount from a long character string values.sequence. This does not work (suggested by the help file):
values.trialtype = substring(values.substring, values.trialcount,values.trialcount);

Neither does this (suggested by the inquisit tooltip):
values.trialtype = values.sequence.substring(values.trialcount,1);

What should I do?

By Dave - 9/20/2022

Sercan - 9/20/2022
I am trying to pull a single character at index values.trialcount from a long character string values.sequence. This does not work (suggested by the help file):
values.trialtype = substring(values.substring, values.trialcount,values.trialcount);

Neither does this (suggested by the inquisit tooltip):
values.trialtype = values.sequence.substring(values.trialcount,1);

What should I do?


The substring function works as detailed in the documentation.

https://www.millisecond.com/support/docs/current/html/language/expressions/functions.htm



It takes three parameters:
(1) The string you want to extract a substring from.
(2) The start index of the substring you want to extract within the string. Note that the string index is zero-based.
(3) The length of the substring you want to extract from the string.

That is, with

<values>
/ sequence = "abcd"
</values>


substring(values.sequence, 0, 1)


will return "a"

substring(values.sequence, 1, 1)


will return "b"

substring(values.sequence, 2, 1)


will return "c"

and

substring(values.sequence, 3, 1)


will return "d".

In code:

<values>
/ sequence = "abcd"
/ trialcount = 0
/ substring = ""
</values>

<trial exampletrial>
/ ontrialbegin = [
    values.trialcount += 1;
    values.substring = substring(values.sequence, values.trialcount-1, 1);
]
/ stimulusframes = [1=exampletext]
/ validresponse = (57)
</trial>

<text exampletext>
/ items = ("<%values.substring%>")
</text>

<block exampleblock>
/ trials = [1-4 = exampletrial]
</block>