Have an idea?

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

Perl script to check for straightlining behavior in CBC-Tasks

Hello,

I wrote a Perl script to identify straightlining behavior in CBC-Tasks. The CBC-questions contain 10 tasks with 3 choice sets each.  The script looks like this:

<input type="hidden" name="SetStraightlinerCBC_Dummy" id="SetStraightlinerCBC_Dummy" value="[% Begin Unverified Perl
# Parameters
my $exer = 'CBC';
my $tasks = 10;
my $resp1 = 0;
my $resp2 = 0;
my $resp3 = 0;

# Run
for (my $i = 1; $i <= $tasks; $i++) {
    if (GETVALUE($exer . '_Random' . $i) == 1) {
        $resp1++;
    }
    elsif (GETVALUE($exer . '_Random' . $i) == 2) {
        $resp2++;    
    } 
    else (GETVALUE($exer . '_Random' . $i) == 3) {
        $resp3++;    
    }
}
    if (($resp1 or $resp2 or $resp3) >= 8) {
        return 1;
    }    
    else {
    return 0;    
    }
End Unverified %]"/>


The idea is, to count how often a respondent is choosing the first, the second and the third choice set. This is done by using the variables $resp1, $resp2 and $resp3. If one of the variables is higher than or equals 8, then SetStraightlinerCBC_Dummy = 1, otherwise it should equal 0.

Unfortunately the code isn't working and I do not know what the mistake could be.

I am looking forward for your help!
asked Aug 4 by ulricin (140 points)
It looks like you're close.  This is not recognized Perl:

($resp1 or $resp2 or $resp3) >= 8


Try this instead:

$resp1 >= 8 || $resp2 >= 8 || $resp3 >= 8
Hello,

thank you for your answer! I tried it, but unfortunately it is still not working. It looks like there is also a problem with the ">=" operator. If I write it like this

if ($resp1 < 9 && $resp2 < 9 && $resp3 < 9)


the perl gets recognized, but I still get an Error when I test the survey. Do you have any other ideas where the problem might be?
">=" is a valid Perl operator.

If you're getting an error with unverified Perl, it is most likely an error #132.  If so, you can find additional info about what went wrong in the admin module.  Can you share that info?
Hi Zachary,

this is the info I found in the admin module:
System Error:
syntax error at (eval 42) line 16, near "else (" syntax error at (eval 42) line 19, near "} }"

Thank you for your help!
The braces in your original post look fine.  Can you post the full code you are trying right now that is producing this?
Hi Zachary,

this is the code that is producing this error:

<input type="hidden" name="SetStraightlinerCBC_Dummy" id="SetStraightlinerCBC_Dummy" value="[% Begin Unverified Perl
# Parameters
my $exer = 'CBC';
my $tasks = 10;
my $resp1 = 0;
my $resp2 = 0;
my $resp3 = 0;
 
# Run
for (my $i = 1; $i <= $tasks; $i++) {
    if (GETVALUE($exer . '_Random' . $i) == 1) {
        $resp1++;
    }
    elsif (GETVALUE($exer . '_Random' . $i) == 2) {
        $resp2++;    
    } 
    else (GETVALUE($exer . '_Random' . $i) == 3) {
        $resp3++;    
    }
}
    if ($resp1 < 9 && $resp2 < 9 && $resp3 < 9) {
        return 0;
    }    
    else {
    return 1;    
    }
End Unverified %]"/>
Compare line 17 with line 24.  "else" doesn't support conditional rules - it just runs when the preceding "if" and any "elsif" don't run.  Either replace it with an "elsif" or remove the two parentheses and everything between them on line 17.
Hi Zachary,

thank you very much, it works now!

Best Regards

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.
...