Have an idea?

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

Creating lists from pass-in gender/ethnicity demographic strings for quotas

I was hoping someone could correct my mistake in creating some lists from pass-in gender/ethnicity strings. For gender, a six-character string will be passed into the survey to signify respondents previously obtained choices (male, female, trans, non-binary, other, prefer not to answer) on a registration form. With this method, a 100000 would be passed in for a male, a 010100 for someone selecting female and non-binary. (Ethnicity is also derived from a multi-select question so it also requires a string to be passed from the panel database. Once I get one of my Constructed lists created all the others for Gender and Ethnicity will follow the same basic technique.)
To Construct a Male (or Female, or Trans, etc.) list, I’d like to use the QG pass-in string’s incoming one or zero to indicate true or false. I setup a Fixed parent list called GenderO (1,0) so an appropriate value can be pulled from it. I’m using an array below so I can identify the right position in the string for each of my MaleO, FemaleO, TransO, etc Constructed lists. Once these lists are created, my Quotas will poll them.

So, the below Perl script is intended to populate the MaleO list with a one if the first digit (of the passed in string) is a one and a zero if the first digit is zero. (The Constructed FemaleO list will do the same for the second digit, TransO third, etc.)

Begin Unverified Perl

# Parameters
my $plist = 'GenderO';
my @array = 'QG';

if(GETVALUE("@array0") == 1)
{
 ADD("GenderO", 1);
}
if(GETVALUE("@array0") == 0)
{
 ADD("GenderO", 2);
}

End Unverified

When I test the above code, I get Sawtooth Error 132 – Scripting Error.

I’m guessing I may have the syntax off. (I’m not a programmer and this is the first time I’ve used an array.) Can anyone see what is wrong with the above code and suggest a correction?
Thanks,
asked Jul 26 by MMidghall (205 points)

1 Answer

0 votes
If QG is the name of your pass-in field, then calling GetValue on it won't automatically return an array.  It will return a number / string (Perl is a little weird on that distinction) representing whatever value got passed in.  For your example, this might be 010100 / "010100".

If we want to turn this into an array where each item represents a character of the passed-in value, we could use Perl's split function.  You could add this example to a question text in your survey.  It will print out just the first character of the value.

[% Begin Unverified Perl
my $input = GETVALUE('GenderO');
my @characters = split(//, $input);
return $characters[0];
End Unverified %]


It looks like your existing code may only be interested in getting a single character from the value anyway, though, so this may be overkill.  This example uses Perl's substr function to pick out the Nth character of a string, then we can use it in your conditionals to add just the desired item to the constructed list.

my $input = GETVALUE('GenderO');
my $index = 0;

my $flag = substr($input, $index, 1);
if ($flag == 1) {
    ADD($plist, 1);
}
else {
    ADD($plist, 2);
}


In both of these code examples, the 0 can be adjusted to represent whichever character of the string we're interested in.

That should hopefully get you going, but on a tangential note, it does surprise me to use constructed lists to store this data.  The option that first came to my mind would be to add additional pass-in fields besides QG.  Each one would represent just one of QG's data points, so they would only need to record 0 or 1.  If such a solution sounds like it would be useful to you, I could help write the script that would translate QG's data into these fields.
answered Jul 26 by Zachary Platinum Sawtooth Software, Inc. (209,175 points)
...