PennController for IBEX › Forums › Support › Variable Math and Text Concatenation
- This topic has 5 replies, 3 voices, and was last updated 4 years, 7 months ago by Jeremy.
-
AuthorPosts
-
April 20, 2020 at 8:44 pm #5094glossaphileParticipant
How can I do math on two or more variables and store the result in another variable? Similarly, how can I concatenate text elements into a new text element?
I’m trying to get my experiment to keep track of the proportion of correct responses and report it to the subject at the end of each trial as a percentage.
April 20, 2020 at 10:51 pm #5095JeremyKeymasterHi,
Math operations on Var elements are not totally straightforward if you’re trying to keep plain javascript minimal, here’s an example:
newTrial( newVar("dividend"),newVar("divisor") , newTextInput() .before( newText("Dividend:") ) .print() .wait() .setVar("dividend") .remove() , newTextInput() .before( newText("Divisor:") ) .print() .wait() .setVar("divisor") .remove() , newVar("result") .set( getVar("dividend") ) .set( v=> v / getVar("divisor").value ) , newText() .text( getVar("dividend") ) .after( newText(" divided by " ) ) // see msg below re. spaces .after( newText().text(getVar("divisor")) ) .after( newText(" makes ") ) // see msg below re. spaces .after( newText().text(getVar("result")) ) .print() , newButton("ok").print().wait() )
As you can see I had to use .value to retrieve the value of another Var element inside the v => ... expression.
The code above should also give you a good idea of how to concatenate Text elements.
Jeremy
April 21, 2020 at 4:31 pm #5098glossaphileParticipantThanks for your ever-prompt responses, Jeremy! I’ve almost accomplished what I need. The only problem now is that the computer seems to be trimming off blank space at the beginning and ends of the fixed strings. For example, my code to report reaction time to the user looks like this.
newVar("sRT").set(getVar("RT")) .set(v => v/1000) , newText("secs","000").text(getVar("sRT")) , newText("Reaction Time: ") .after(getText("secs")) .after(newText(" seconds")) .print() ,
Using 653 ms as an example, I should be getting “Reaction Time: 0.653 seconds,” but instead I’m getting, “Reaction Time:0.653seconds,” with the space before and after the number missing from the display despite being present in the code.
April 21, 2020 at 5:03 pm #5099JeremyKeymasterAh, sorry, there were supposed to be HTML entities in my code above but I guess the forum somehow stripped them away
I should make initial and last space characters count in the next releases of PennController. In the meantime, replace the space characters at the beginning or at the end of a Text element with
Jeremy
June 2, 2020 at 1:51 pm #5563NcomeauParticipantDo you have to perform variable/text concatenation like this within a trial? I’m struggling a bit with constructing URL text to pass into my preloadzip function. Ideally I’d be able to insert the value back from getURLParameter (I’d imagine this is a variable and not text?) into some text like “myURL_” + getURLParameter(“withsquare”) +”.zip”, but I always seem to get that invalid URL message in my log…
June 2, 2020 at 1:56 pm #5564JeremyKeymasterYou can definitely do what you describe, e.g.:
PreloadZip("https://my.server/path/to/folder/archive-"+GetURLParameter("withsquare")+".zip")
This will fetch file https://my.server/path/to/folder/archive-2.zip if you run your experiment with withsquare=2
Jeremy
-
AuthorPosts
- You must be logged in to reply to this topic.