Have an idea?

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

Perl Scripting with loop


I've created and tested what should be a simple code to assign a random number between 1 & 12 in Perl.

Using jdoodle (https://www.jdoodle.com/execute-perl-online) I have tested the following code and it works fine:

my $version = int rand(2);

my @numbers = (1 .. 12);
my @thenumber = 0;

while (@numbers) {
  my $index = rand(@numbers);
  my $pick = splice(@numbers, $index, 1);
  $picktext = "$pick";
  $thenumber = $thenumber + 1;
  print "Image". $thenumber .": " . "$picktext" . ", ";

print "Version: " . "$version";

So at that point I thought I would convert into Sawtooth:

[% Begin Unverified Perl
    my $version = int rand(2);
    SETVALUE ("VersionNum", $version);
    my @numbers = (1 .. 12);
    my @TheNum = 0;
    my $picktext = "TEXT";
    my $TheNumText = "TEXT";
    while (@numbers) {
        my $index = rand(@numbers);
        my $pick = splice(@numbers, $index, 1);
        $picktext = "$pick";
        $TheNum = $TheNum + 1;
        $TheNumText = $TheNum;

End Unverified %]

This at that point does not work, I've tried countless different things, but I seem to not be having any luck at all.  The Image + NumText is a Pass-In Field, VersionNum is working fine.  The problem I have is that if I force the number to fill in Image1, nothing is filled in, so it looks like it's erroring but not actually presenting a scripting error which is not helping, I only see the issue when I generate test data and see blanks.

Any ideas?  I'm sure this is going to be something silly (which means that this is going to involve a huge rewrite)

asked Jan 16 by crawliano (260 points)
Some simple SSI Script will assign a random number between 1-12.

Pop this into the footer of one of your survey questions ...

Define RandNum as a whole number in your pass-in fields.
Thanks, but I need to assign a unique random number across 12 variables.  Will this take care of that?
This is just doing the one random number. Sorry, I didn't see that requirement in your post.

I'm sure there are a bunch of methods to do this - Perl, JavaScript, etc.

One way around this is via a constructed list.

Define a parent list and call it NumberList.

1-Number 1
2-Number 2
11-Number 11
12-Number 12

Now create a constructed list and call it NumberConList ...

The constructed list NumberConList now stores 12 randomised numbers / labels.

To access the random numbers, simply use the following ...

To access the 1st random number, use [%ListValue(NumberConList,1)%].
To access the 2nd random number, use [%ListValue(NumberConList,2)%].
To access the 3rd random number, use [%ListValue(NumberConList,3)%].
To access the 12th random number, use [%ListValue(NumberConList,12)%].

and so on ...

For analysis purposes, you can simply export NumberConList also.

Does that help?
Not overly, that method risks duplication.  The original code I posted (which worked on JDoodle, just not in Sawtooth) created 12 unique random numbers between 1&12.  I've got to create 12 images and present them in a random order before asking questions to avoid biased responses.

So what I wanted to do is rather than print the result to screen, was to write out to the pass-in variable one by one.  The "VersionNum" script at the top works perfectly mainly because it's simplistic and boolean, I've run a test on 1,000 respondents and the result came up 50/50.  So what I wanted to do is script a loop to create a unique random number between 1&12 and put the results into the appropriate variables so I would end up with (for example)

Image1: 2
Image2: 5
Image3: 12
Image4: 1
Image5: 11
Image6: 6
Image7: 10
Image8: 3
Image9: 4
Image10: 8
Image11: 7
Image12: 9

And so on...
The constructed list method will not encounter duplication. Constructed lists restrict this as a default property of their behaviour.

Certainly give it a test drive to gain peace of mind.

You can create 12 random number variables in the Pass-in fields as follows ...


This will work for your images.
The SetValue commands / script can be placed in the footer of any survey question. Make sure the question you do insert them in is not skipped.
Looking over your code, did you mean to declare "TheNum" as a scalar rather than as an array?

my @TheNum = 0;

my $TheNum = 0;
Of course I did(n't)!

I knew it was something silly and there's the answer, works a treat!

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.