Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Variables inside SSI Script

I"m trying to create a custom verification in SSI 6.6.18 that creates an array of the brands in a List per the following:

//loop to push the brand list values into the MainList array
for (a=1, a <= i, a++){
    MainList.push([%  LISTVALUE(brandlist,i)  %]);
    }

However, I get an error. Are variables for SSI script like this not supported in 6.6.1 (or at all)? Suffice to say...do I need to hardcode this every time I make this script in a new survey?
asked Jan 7 by Corey1975 Bronze (720 points)

1 Answer

+1 vote
Server operations like Sawtooth Script are performed before any client operations like JavaScript, so you cannot pass JavaScript variables into Sawtooth Script like you have done with "i."

Your code snippet does not show how "i" is set.  Depending on that, the best solution to your problem may be to replace your loop in JavaScript with a loop in unverified Perl.  Something akin to this:

var MainList = [% Begin Unverified Perl
    my $str = '[';
    my $sep = '';
    for (my $i = 1; $i <= 10; $i++) {
        $str .= $sep . LISTVALUE('brandlist', $i);
        $sep = ',';
    }
    $str .= ']';
    return $str;
End Unverified %];
answered Jan 7 by Zachary Platinum Sawtooth Software, Inc. (91,300 points)
The variable i is created as follows:

Var i = [% ListLength(brandlist) %]; //which does work

It looks like the code snippet above will generate a string variable with a “,” as a separator as opposed to an array. Or does the format resolve to an array im javascript?
Great.  "i" is generated with Sawtooth Script, so adding that function to my code above shouldn't be a problem.

for (my $i = 1; $i <= LISTLENGTH('brandlist'); $i++) {


You're correct that the Perl is creating an array-looking string rather than an actual array.  On line 9, the contents of the string are passed to the surrounding JavaScript.  The end result is a line of JavaScript code that initializes MainList with an array, like this:

var MainList = [1, 2, 3, 4];
...