Have an idea?

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

Using Perl to assign numeric value to a text pass-in

Hi,

I have a pass-in variable from the URL ("brand") which has one of a number of known text values (e.g., "Costco," "Walmart," etc.). Later, I have Q4 referencing other brands, and I need the "brand" value removed (e.g., if brand=Costco, Costco would be removed from the Q4 list).

I was thinking to use Perl (see below) to set a numeric value to a hidden free format question ("poi") based on the string text in the "brand" field.

So my "poi" question has variables like this:

<input name="poi_Costco" id="poi_Costco" type="hidden">


Then on the page after that hidden question, I have this Perl code in the footer:

[% Begin Unverified Perl
if(VALUE("brand") eq "Costco"){
    SETVALUE("poi", 1);
}

End Unverified %]



However, I'm getting an error when it should be loading the page with the Perl code.

What am I missing?

Note: I'm basing this plan off of this post: https://www.sawtoothsoftware.com/forum/5488/assign-a-value-to-a-string-variable?show=5488#q5488
asked Mar 14, 2014 by cschlosser Bronze (1,005 points)

1 Answer

0 votes
Here is an extract from the Sawtooth HELP for the SetValue function ...

SetValue (QuestionName, Value)

SetValue() allows you to save values to existing variables in the database (it does not create new variables on the fly within the database).  For example:

[%SetValue(Q1, 5) %]

sets the value for Q1 to 5 in the database.

Currently we do not have a place in the SSI Web interface where you can specify variable names.  You could use an existing question in the question list (such as a Free Format hidden variable).  But, we suggest you use Pass-In Fields (located in the Start question) to set up variables that can be used by SetValue().  For example you could create a Pass-In field named "age".  You could then set the value for "age" to 45 midway through the survey using the following command:

[%SetValue(age, 45) %]

I suspect you may have an issue with your "poi variable (where you want to store the coded value)?

Go to the pass-in-field and simply define "poi" as a whole number variable.

Be careful with the naming of your variables as Perl is case sensitive. Your Perl script looks okay.

When constructing your brand list later on (assume Q5BrandList which has 1-10 codes) and you want to remove the "poi" brand from the total list, use something like this in your constructed list ...

ADD(Q5BrandList,1,10)
Remove(Q5BrandList,poi)


That will work nicely.
answered Mar 15, 2014 by Paul Moon Platinum (65,950 points)
edited Mar 18, 2014 by Paul Moon
Thanks, Paul! You, as always, have the answer!  The problem was with my poi hidden variable. It was not named as it should have been to match the SetValue script. Thanks!
...