Have an idea?

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

List Construction PERL Error

Has anyone ever seen this before...

System Error Message: Can't use string ("0") as a HASH ref while "strict refs" in use at /home/talism10/SceneSave.talismaninsights.com/cgi-bin/authlib6_6_18.pl line 12, <GEN1> line 113.
 |  params= hid_test_mode=>1, hid_pagenum=>31, hid_backup=>ssob,t,346412,30,1, hid_respnum=>t, hid_checksum=>346412, hid_studyname=>ssob, hid_javascript=>1, hid_test_mode_settings=>,0,0,0,0,,0, hid_q_q3test=>q3test,  IP= 24.1.100.83 ,  client= Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36

I'm using the following PERL code to build a list based on responses to a freeform item (a droppable sort task). I tried a "<" comparator before and was getting a longer error message with each item explicated. I tried the int() function to force integer recognition but to not avail (hence the jumble below).

Code is below:

Begin Unverified Perl

my $item1 = VALUE("Q3_Item1");
my $item2 = int(VALUE("Q3_Item2"));
my $item3 = int(VALUE("Q3_Item3"));
my $item4 = int(VALUE("Q3_Item4"));
my $item5 = int(VALUE("Q3_Item5"));
my $item6 = int(VALUE("Q3_Item6"));
my $item7 = int(VALUE("Q3_Item7"));
my $item8 = VALUE("Q3_Item8");
my $item9 = int(VALUE("Q3_Item9"));
my $item10 = int(VALUE("Q3_Item10"));
my $item11 = int(VALUE("Q3_Item11"));
my $item12 = int(VALUE("Q3_Item12"));
my $item13 = int(VALUE("Q3_Item13"));
my $item14 = int(VALUE("Q3_Item14"));
my $item15 = int(VALUE("Q3_Item15"));
my $item16 = int(VALUE("Q3_Item16"));
my $item17 = int(VALUE("Q3_Item17"));
my $item18 = int(VALUE("Q3_Item18"));
my $item19 = int(VALUE("Q3_Item19"));
my $item20 = int(VALUE("Q3_Item20"));
my $item21 = int(VALUE("Q3_Item21"));
my $item22 = int(VALUE("Q3_Item22"));
my $item23 = int(VALUE("Q3_Item23"));
my $item24 = int(VALUE("Q3_Item24"));

if($item1 == 1 || $item1 == 2) {
    ADD("Q3Base,1");
    }
if($item2 == 1 || $item2 == 2) {
    ADD("Q3Base,2");
    }
if($item3== 1 || $item3 == 2) {
    ADD("Q3Base,3");
    }
if($item4== 1 || $item4 == 2) {
    ADD("Q3Base,4");
    }
if($item5== 1 || $item5 == 2) {
    ADD("Q3Base,5");
    }
if($item6== 1 || $item6 == 2) {
    ADD("Q3Base,6");
    }
if($item7== 1 || $item7 == 2) {
    ADD("Q3Base,7");
    }
if($item8== 1 || $item8 == 2) {
    ADD("Q3Base,8");
    }
if($item9== 1 || $item9 == 2) {
    ADD("Q3Base,9");
    }
if($item10== 1 || $item10 == 2) {
    ADD("Q3Base,10");
    }
if($item11== 1 || $item11 == 2) {
    ADD("Q3Base,11");
    }
if($item12== 1 || $item12 == 2) {
    ADD("Q3Base,12");
    }
if($item13== 1 || $item13 == 2) {
    ADD("Q3Base,13");
    }
if($item14== 1 || $item14 == 2) {
    ADD("Q3Base,14");
    }
if($item15== 1 || $item15 == 2) {
    ADD("Q3Base,15");
    }
if($item16== 1 || $item16 == 2) {
    ADD("Q3Base,16");
    }
if($item17== 1 || $item17 == 2) {
    ADD("Q3Base,17");
    }
if($item18== 1 || $item18 == 2) {
    ADD("Q3Base,18");
    }
if($item19== 1 || $item19 == 2) {
    ADD("Q3Base,19");
    }
if($item20== 1 || $item20 == 2) {
    ADD("Q3Base,20");
    }
if($item21== 1 || $item21 == 2) {
    ADD("Q3Base,21");
    }
if($item22== 1 || $item22 == 2) {
    ADD("Q3Base,22");
    }
if($item23== 1 || $item23 == 2) {
    ADD("Q3Base,23");
    }
if($item24== 1 || $item24 == 2) {
    ADD("Q3Base,24");
    }
RANDOMIZE();

End Unverified
closed with the note: I'm an idiot and had the final " for the ADD in the wrong place...
asked Dec 28, 2018 by Corey1975 Bronze (740 points)
closed Dec 28, 2018 by Corey1975
I don't have your free format question details in front of me. So I'll go with this ...

The above script is a long winded approach and prone to errors (due to the amount of script). A "for loop" approach would come in handy here and applies a more efficient solution.

I suspect your "if statements" should also be "elsif statements" also?

Try this solution ...
Begin Unverified Perl
 
 my $i=1;

 for($i=1; $i<=24; $i++)
  {
   if (VALUE("Q3_Item".$i)==1 || VALUE("Q3_Item".$i)==2)
    {  
     ADD("Q3Base",$i);
    }  
  }
 
 RANDOMIZE();
 
End Unverified

Hope that gets your sorted out. Happy New Year Corey!
THanks. That makes the code more efficient but my error was this:

ADD("Q3Base, $i"); note the position of the final quote...it was in the wrong place.
Yes, that syntax error will generate an error. Move the quote as follows ...

ADD("Q3Base", $i);
...