Have an idea?

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

I would like to save the index values and names of selected check boxes in a grid in separate pass-in fields.

Here is the code I have written so far:

 [%
 Begin Unverified Perl
 # VARIABLES: createOrigin, createDestination, createOriginIndex, createDestinationIndex, createTrips, createNoTrips
 # OBJECTIVE: Obtain and store names and indexes (of grid) Origins and Destinations respondents choose from OriginDestination2 grid question
 # INPUTS: 1. OriginDestination2 grid outputs

# initiate createTrips to 0
SETVALUE("createTrips",0);

# Matrix of trips

my @matrixTrips = ([99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99]);


# Set regions/cities
my @cities = ("Boston","Rhode Island","New Haven","New York","Trenton","Philadelphia","Wilmington","Baltimore","Washington","New London","Stamford","Hartford","Springfield","Poughkeepsie","Lancaster","Harrisburg","Albany, NY","Virginia","Cape Cod & others","External 1","External 2","External 3","External 4"); 

# initiate trip counters
my $numTrips;

for (my $i = 1; $i <= 23; $i++) {
    for (my $j = 1; $j <= 23; $j++) {
    if (($matrixTrips[$i-1][$j-1]==1) & (GETVALUE("OriginDestination2" . $i . "_c" . $j)==1)) {
        $numTrips++;
        }
    }
}

# Elect random trip number among trips
my $rand;

if ($numTrips > 0) {
    # set createTrips to 1 if there are any trips
    SETVALUE("createTrips",1);
    if ($numTrips == 1) {
    $rand = 1;
    } else {
    $rand = RANDNUM(0,1,$numTrips);
    }
}

# In the previous question, respondents may choose more than 1 Origin-Destination pair. In case they do, we want to randomly select any of those pairs and store that random O-D pair details in the Pass-in Variables

# initiate Index values for Origin and Destination
my $rO;
my $cD;
my $Counter = 1;

 
# iterate through all trips to find the selected one
for (my $ii = 1; $ii <= 23; $ii++) {
    for (my $jj = 1; $jj <= 23; $jj++) {
        if (($matrixTrips[$ii-1][$jj-1]==1) & (GETVALUE("OriginDestination2_r" . $ii . "_c" . $jj)==1)) {
            if ($Counter < $rand) {
            $Counter++;
            } elsif ($Counter == $rand) {
            $Counter++;
            $rO = $ii;
            $cD = $jj;
            }
        } 
    }         
}  

if ($numTrips > 0) {
    SETVALUE("createOrigin",$cities[$rO-1]);
    SETVALUE("createDestination",$cities[$cD-1]);
    SETVALUE("createOriginIndex",$rO);
    SETVALUE("createDestinationIndex",$cD);
}    

if($numTrips < 0) {
    SETVALUE("createNoTrips",1);
}
    
 End Unverified
%]


but this code is throwing an error ("Sawtooth Error #132")
asked Jan 15 by Shukrit
Error #132 just means that something went wrong when running unverified Perl.  You can get more information about what happened in the admin module.

A few thoughts without seeing the admin module:

* Line 34 appears to have a malformed variable name.  Where is the "_r"?
* Line 49 is firing RandNum with a fixed seed.  Is this what you want?  For a specific $numTrips, this line will always produce the exact same $rand.  Perhaps the first parameter should be RespNum() instead?
* I'm not sure @matrixTrips is necessary.  Would it be cleaner to replace "$matrixTrips[$i-1][$j-1]==1" with "$i - 1 != $j - 1"?

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