Have an idea?

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

Store number validation | Faster code?

Hi everyone,

I have a survey where respondents are asked to enter their store number. The retail network has over 2000 stores. I want to validate whether the respondent enters an existing store number or not.

The valid store numbers are listed in a list called "Filialen".  They can consist of 1 to 4 digits.
 
Participants enter the store number in the question "ID1".


I'm using this code in a constructed list called "FilialCheck" to loop over the list for verification. If ListLength(FilialCheck)=0, participants are screened out. It works, but very slowly:

Begin Unverified Perl
 
 my $i=1;
 my $filiale = VALUE("ID1");
 
 for($i=1; $i<=LISTLENGTH("Filialen"); $i++)
  {
   if ($filiale eq LISTLABEL("Filialen",$i))
    {  
     ADD("Filialen",$i);
     last;
    }  
  }
 
End Unverified


Is there a way to make the code run faster?
asked May 31, 2021 by Kathrin (415 points)
edited May 31, 2021 by Kathrin
I pulled the  LISTLENGTH("Filialen") part out of the loop specification, that made it significantly better. New code:

Begin Unverified Perl
 
 my $i=1;
 my $filiale = VALUE("ID1");
 my $anzahl = LISTLENGTH("Filialen");
 
 for($i=1; $i<=$anzahl; $i++)
  {
   if ($filiale eq LISTLABEL("Filialen",$i))
    {  
     ADD("Filialen",$i);
     last;
    }  
  }
 
End Unverified

1 Answer

0 votes
Great result.

That should certainly make a difference as it means you are calling upon that command just the once before entering the loop, rather than the maximum 2000 times (depending on where the match may occur as the "last" command can exit you from the loop beforehand).

And as Zachary mentioned in that earlier forum post, the hash array approach is something to consider as a further improvement if required for a future similar problem where the list is even larger.

I have my own local server and it is very quick with high specifications set, so I don't encounter the speed issue you witnessed. I have seen this on other slower servers, so I appreciate the need for speedy scripting.
answered May 31, 2021 by Paul Moon Platinum (95,750 points)
Thanks for your reply.

I am new to hashes, is there a way how I can turn the predefined list "Filialen" into a hash? As I understand it, hashes deal with value pairs, but I only want to know whether the numer exists in the list or not?
return exists $zipCodes{GETVALUE($zipCodeQuestion)};

as I understand it, this will return true, which if used as a skip logic would mean that participants are screened out when they enter a valid zipcode, right? How would I negate this expression?
Do you need to keep the predefined list at all, or can the values be moved from the predefined list into the hash code?

We should be able to handle the negation with "!":

return !(exists $zipCodes{GETVALUE($zipCodeQuestion)});
If you use the script, add in the zip code list and a text editor that enables key macros, you can swiftly put that Zachary script neatly together and insert it as a skip in to Sawtooth.

Thanks for chipping in with your comment too Zachary.
...