Have an idea?

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

Insert multiple list members in constructed list with Perl

Hello everybody,

we are using SSI Web and I need to display certain list members in a constructed list using unverified perl. The code is pasted below. My problem: I need to insert two models from the same list. I put in this code to display two:

INSERT (1, "CarModels", $random_numberA);
INSERT (2, "CarModels", $random_numberA);

However only one model is shown. If I add +1 after the second entry of  $random_numberA it works. However this is against my logic I defined in the while term.

I appreciate your comments.



begin unverified perl

my $rangeA = 29;
my $minimumA = 1;
my $random_numberA = int(rand($rangeA)) + $minimumA;
my $own = VALUE("Q1");

while ($random_numberA==1 or $random_numberA==2 or $random_numberA==3 or $random_numberA==5 or $random_numberA==6 or $random_numberA==7 or $random_numberA==8 or $random_numberA==9 or $random_numberA==10 or $random_numberA==12 or $random_numberA==13 or $random_numberA==14 or $random_numberA==15 or $random_numberA==17 or $random_numberA==18 or $random_numberA==19 or $random_numberA==20 or $random_numberA==21 or $random_numberA==23 or $random_numberA==24 or $random_numberA==25 or $random_numberA==26 or $random_numberA==27 or $random_numberA==28)
$random_numberA = int(rand($rangeA)) + $minimumA;
if ($own==22 or $own==29 or $own==4 or $own==11 or $own==16)
INSERT (1, "CarModels", $own);
INSERT (2, "CarModels", $random_numberA);

INSERT (1, "CarModels", $random_numberA);
INSERT (2, "CarModels", $random_numberA);

ADD ("CarModels", 11);

end unverified
asked Jun 17, 2015 by Josh (240 points)

1 Answer

0 votes
If you try to add the same code into a constructed list, Sawtooth Software will automatically "or" the two codes resulting in only one of that code appearing.

You have the following code under the else statement ...
INSERT (1, "CarModels", $random_numberA); 
INSERT (2, "CarModels", $random_numberA); 

These two instructions will simply add the same code twice resulting in it appearing only once.

Note the following list building rules which I extracted from the Sawtooth Software HELP ...

#Note point 4.

Rules Governing List Operations ...

1. All constructed lists are initially empty.
2. All members added to a constructed list must come from a common parent list.
3. You may add members from another constructed list to a constructed list, as long as both constructed lists share the same parent predefined list.
4. A member can only appear once in the constructed list (if you try to add a member after it already exists on a constructed list, it will not be added again).
5. List building commands are executed sequentially.
6. A question using an empty constructed list (as response options) is skipped.

When you view questions involving constructed lists in Preview mode, all members from the parent list are displayed (since we don't know how the respondent has answered previous questions).

I'm struggling to understand why you want to insert the same code in positions 1 and 2? If you wanted to, there is another way around it. You can define a new parent list with codes defined as ...

1 [%ListLabel(ConstructedListName,1)%]
2 [%ListLabel(ConstructedListName,1)%]

Does this help?
answered Jun 17, 2015 by Paul Moon Platinum (81,475 points)
And be careful with case sensitivity and Perl. Functions and variables need to be specified correctly for your Perl script to function correctly. There are some neat examples in the Sawtooth Software HELP and on the forum from various users.
Thank you for your answer, I understand why it does not work. Actually I need to insert to models that were randomly chosen under the predefined conditions. Always three models should be displayed:

Respondents own car model if part of our collection
Random car model according to predefined terms
Model 11


Random car model according to predefined terms
Another random car model according to predefined terms
Modell 11

I figure I might need to add another random number for the second model that is <> random number A

However I do not understand why the following function does not work:

INSERT (1, "CarModels", $own);
INSERT (2, "CarModels", $random_numberA);

Here again only the first model (own) is shown.
Yes, the random numbers must be different so that you are adding two different codes.

Interesting you are using the rand and int Perl functions and not the SSI Script functions like SysRand or RandNum. Still, it's another option and a neat way to do it.