Have an idea?

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

Counting the number of times a column is selected in a Grid Question

I have used Paul Moon’s suggestion on populating a Grid question with those rows from another Grid Q for which a particular column [4] was selected
https://sawtoothsoftware.com/forum/6930/constructed-list-from-multi-select-grid-question?show=6930#q6930

My Constructed List code looks like this:

Begin Unverified Perl
 my $i=1;
for ($i=1; $i<=99; $i++)
 {
  if (VALUE("attend1_r".$i."_c4")==1)
   {
    ADD("attend1RowList",$i);
   }
 }
End Unverified


and works fine.

I also want to count the number of col4 selections made, and then skip if that value is zero.
I created a pass-in variable (y1nosee) and forced in some code in the Constructed List to try to create a running count [a1] of col4 selections and assign that count to y1nosee when all rows were evaluated:

Begin Unverified Perl
my $a1=0;
my $i=1;
for ($i=1; $i<=99; $i++)
 {
  if (VALUE("attend1_r".$i."_c4")==1)
   {
     ADD("attend1RowList",$i);
     a1=$a1+1;
   }
 }
SETVALUE("y1nosee",$a1);
End Unverified
 


But that didn’t work.
So I tried similar code in a footer on the page after the Grid Q:

[% Begin Unverified Perl
my $a1=0;
my $j=1;
for ($j=1; $j<=99; $j++)
 {
  if (VALUE("attend1_r".$j."_c4")==1)
   {
    a1=$a1+1;
   }
 }
SETVALUE("y1nosee",$a1); 
End Unverified %]


That gave an Error#132

The only way I could do this was when to write it out for each row of the GridQ.

[% Begin Unverified Perl
 my $a=0;
 if(VALUE("attend1_r1_c4")==1)
{
  $a=$a+1;
}
 if(VALUE("attend1_r2_c4")==1)
{
  $a=$a+1;
}
  if(VALUE("attend1_r3_c4")==1)
{
  $a=$a+1;
}
  if(VALUE("attend1_r4_c4")==1)
{
  $a=$a+1;
}
 SETVALUE("y11nosee",$a);
End Unverified %] 



Can anyone advise on achieving this without writing it out in full – I have many rows and it’s very inelegant

thanks
Dan
asked Nov 6, 2016 by dan r Bronze (1,550 points)
I think you're missing a dollar sign, causing the error #132s.  You have this:
a1=$a1+1;

when I believe you need this:
$a1=$a1+1;
Thanks Zachary, there was indeed a missing $

So now this works if put in the footer of a question on the page after the Grid Q:

[% Begin Unverified Perl
my $a1=0;
my $j=1;
for ($j=1; $j<=99; $j++)
 {
  if (VALUE("attend1_r".$j."_c4")==1)
   {
   $a1=$a1+1;
   }
 }
SETVALUE("y1nosee",$a1); 
End Unverified %]


And I can base skip logic on the value of y1nosee
The snag with that is having a page with no substantive material on it, to allow the calculation in the footer and skip accordingly.

I hoped the Constructed List code (with my missing $ inserted) might avoid this.

Begin Unverified Perl
 my $a11=0;
 my $i=1;
for ($i=1; $i<=99; $i++)
 {
  if (VALUE("attend1_r".$i."_c4")==1)
   {
    ADD("attend1RowList",$i);
    $a11=$a11+1;
   }
 }
SETVALUE("y11nosee",$a11);
End Unverified


But the code doesn’t generate a value for y11nosee – no error, just no value generated / displayed when I list [%y11nosee%]

So, thanks for identifying the missing $

Is there a way to avoid the additional page after the Grid Q in order to calculate the number of times col 4 was selected and skip accordingly?

thanks again
Dan
One way to do this without the use of the blank page would be to put the code in the skip logic itself.  You would need to add a "return" statement to the end of the code that determined whether or not the skip should occur.  This skip logic will count the number of selected items, set "y1nosee" to that number, and then run the skip if the number is zero:

Begin Unverified Perl
my $a1=0;
my $j=1;
for ($j=1; $j<=99; $j++)
{
    if (VALUE("attend1_r".$j."_c4")==1)
    {
        $a1=$a1+1;
    }
}
SETVALUE("y1nosee", $a1);
return ($a1 == 0);
End Unverified
If that doesn't suit you, you can also look into our JavaScript function SSI_SubmitMe.  If you call this function on a page, the page will submit itself.  This would still involve the respondent going to the empty page, but they would be quickly moved to the next page.
Thanks Zachary, I will look into these 2 options

Dan

Your answer

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