Have an idea?

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

Pulling specific info out of a nested loop

Hi all

Got a curly one.

First we ask on what hours someone does something (24 slots).

Then we ask, for each of the slots, the number of times they did it (occasions)

Then for each of those occasions, we ask a category, and for certain categories we also ask a brand.

So there is a lot being asked.  I have the outer loop (running from the slots they used to the end of the brand list) as B3Outer and the other one running inside it (from the number of occasions to the end of the brand list) as B3Inner.

THEN I need to assign each of the 24 hour slots into dayparts (morning, mid-morning, afternoon, etc.

THEN I choose one of these dayparts for which they have filled in a time slot (least fill).  Having chosen this, I go back and randomly select one of the hourly slots they filled in that fit that daypart.  Then I need to choose one of the occasions within that hourly slot and extract the category, and if applicable, the brand.

Here is the code I wrote to try to choose an occasion within the hour slot:

Begin Unverified Perl

my $hour = VALUE("OccasionHour");
my $i = 0;

for($i = 1; $i <= 24; $i++)
{
    if($hour == $i && VALUE("B3a."$i) == 1)
    {
        ADD("B3aList",1);
    }
    elsif($hour == $i && VALUE("B3a."$i) == 2)
    {
        ADD("B3aList",1,2);
    }
    else
    {
        ADD("B3aList",1,3);
    }    
}    

End Unverified

Randomize()
ListMax(1)


It falls over.  And the database is so big I can't even get into the admin module to see what is happening (which is another problem entirely).

Can anyone help?  I'm on 9.10.1
asked Nov 1, 2022 by Marion Silver (5,400 points)
I feel like there's some context missing here.  You talk about "slots" - is that a checkbox-type select question, or a grid question where respondents are selecting an item for each hour, or something else?  The code you provided references a "B3a" question, but I don't know what this question is or what problem the constructed list is having.

Could you perhaps provide an example or two of different values a respondent might have for OccasionHour and B3a and how you'd expect the constructed list to look for that respondent?
Hi Zachary ... the support guys are working on this one for me as well.

The hourly slots are multi-select (Question B1).  For each hour chosen they get asked number of occasions (Question B3a - start of loop called B3Outer).  For each occasion they get asked category (multi-select question B3b - start of loop called B3Inner) and - for some categories - brand (question B4)

So someone could choose 3 hourly slots at B1.  Say they select 0900-0959, 1400-1459 and 1800-1859.  They each get asked number of occasions.  So in our example, they say, for 0900-0959 there was 1 occasion.  They then choose 1 category but that category doesn't need a brand associated.  For the 1400-1459 slot they had 2 occasions.  For the first, they choose 2 categories.  For one of these categories we ask brand.  The other category isn't one we need a brand for.  For the 1800-1859 slot they also had just one occasion.  This one had 2 categories chosen, both of which need a brand to be chosen.

So that's the double loop.

So then I need to choose one of those occasions (complete with one of the categories and brand if there is one) to deep dive on.
So I guess what I really need to know is how to pull specific info out of a double loop.
Occasions is a multi-select question as well, right?  If I'm understanding right, I don't know if a constructed list would be required for this.  If we want to select the choice by least fill, it sounds like we'll need a quota question with cells like these:

Morning & occasion 1
Morning & occasion 2
Mid-morning & occasion 1
Mid-morning & occasion 2
...


The logic for each quota cell would need to examine the responses to the earlier questions, naturally.  Then, once a quota cell is established for the respondent, we could use Sawtooth Script to pull out category and brand that was selected.  Is this approach sounding like we're going down the right direction?

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