Have an idea?

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

Refrencing a pass-in variable in PERL

I have a survey that terminates into another survey, the latter being sort of repository for points. This repository already has some values stored in it, before this current survey terminates into it. I can see the values in the admin module, but seem to be having a difficult time getting the value to work in my math.

The variable in the database is labeled "BaselineQ1" and has a value of of 8-64 depending on the respondent. I need to divide that value by 12, then do some additional math. Right now all that comes out is the addition and multiplication portion, the first and third line of math seem to be getting ignored.

[code]
my $baselineQ1 = GETVALUE("BaselineQ1");

if("Section6" == "done")
{
    my $MEC1total1 = $baselineQ1 / 12;
    my $MEC1total2 = GETVALUE("MEC1transportationCO2") + GETVALUE("MEC1householdCO2") + GETVALUE("MEC1foodCO2") + GETVALUE("MEC1purchasesCO2");
    my $MEC1total3 = $MEC1total1 - $MEC1total2;
    my $MEC1total4 = $MEC1total3 * 2200;
    $MEC1totals = $MEC1total4;
}
my $intVarTotal = $MEC1totals;
my $roundedTotal = sprintf("%.4f", $intVarTotal);
if($intVarTotal > 0)
{
    SETVALUE("MEC1total", $roundedTotal);
}
asked Mar 7, 2017 by Ron H.
A couple of things jump out to me:

1. $MEC1totals is never initialized.  You may need to add this code before the if statement:

my $MEC1totals;


2. When comparing strings in Perl, you should use the "eq" operator instead of "==."

3. When do you want the if statement to run?  The string "Section6" will never be equal to the string "done."
Hi Zachary, thanks for getting back to me.

1. The MEC1totals is initialized above this block of code, sorry didn't paste the code in it's entirety.

2. Noted, but it seems to be working. Will update for cleanliness/correctness sake.

3. Section6 is a pass-in variable that is brought in by the terminate link the survey they are coming from. The value of that variable is set as "done" in the link. This seems to be working for the other blocks of code the follow this one, that are also enclosed in the same if statement.

Here is the code in it's entirety (I apologize ahead of time for the poor syntax - I don't know PERL or JS very well)

[% Begin Unverified Perl

my $MEC1totals = 0;
my $MEC1bonuses = 0;
my $MEC1points = 0;
my $MEC1totalExercise = 0;
my $baselineQ1 = GETVALUE("BaselineQ1");

#Total Points
if("Section6" == "done")
{
    my $MEC1total1 = $baselineQ1 / 12;
    my $MEC1total2 = GETVALUE("MEC1transportationCO2") + GETVALUE("MEC1householdCO2") + GETVALUE("MEC1foodCO2") + GETVALUE("MEC1purchasesCO2"); 
    my $MEC1total3 = $MEC1total1 - $MEC1total2;
    my $MEC1total4 = $MEC1total3 * 2200;
    $MEC1totals = $MEC1total4;
}
my $intVarTotal = $MEC1totals;
my $roundedTotal = sprintf("%.4f", $intVarTotal);
if($intVarTotal > 0)
{
    SETVALUE("MEC1total", $roundedTotal);
}

#Bonuses
if("Section6" == "done")
{
    $MEC1bonuses = GETVALUE("MEC1S1bonus") + GETVALUE("MEC1S2bonus");
}
my $intVarBonus = $MEC1bonuses;
my $roundedBonus = sprintf("%.4f", $intVarBonus);
if($intVarBonus > 0)
{

    SETVALUE("MEC1bonus", $roundedBonus);
}

#Total points
if("Section6" == "done")
{
    $MEC1points = GETVALUE("MEC1bonus") + GETVALUE("MEC1total");
}
my $intVarPoints = $MEC1points;
my $roundedPoints = sprintf("%.4f", $intVarPoints);
if($intVarPoints > 0)
{

    SETVALUE("MEC1points", $roundedPoints);
}

#Total Exercise points
if("Section6" == "done")
{
    $MEC1totalExercise = (GETVALUE("MEC1bonus") + GETVALUE("MEC1total")) * GETVALUE("Multiplier");
}
my $intVarTE = $MEC1totalExercise;
my $roundedTEPoints = sprintf("%.4f", $intVarTE);
if($intVarTE > 0)
{

    SETVALUE("MEC1TEpoints", $roundedTEPoints);
}

End Unverified %]








To refer to your pass-in field, you need to use GETVALUE:

if (GETVALUE("Section6") eq "done")


As is, your Perl is looking at the literal string "Section6," not the database value by that name.
Hi Zachary,

Updated the if statements, but it didn't appear to change anything on the output. The other functions are working and the output is correct, but the first one, that is to divide one variable by 12 just doesn't seem to want to work. It is spitting out the addition portion multiplied by the 2200, but not the rest of it.

[% Begin Unverified Perl

my $MEC1totals = 0;
my $MEC1bonuses = 0;
my $MEC1points = 0;
my $MEC1totalExercise = 0;
my $baselineQ1 = GETVALUE("BaselineQ1");

#Total Points
if(GETVALUE("Section6") eq "done")
{
    my $MEC1total1 = $baselineQ1 / 12;
    my $MEC1total2 = GETVALUE("MEC1transportationCO2") + GETVALUE("MEC1householdCO2") + GETVALUE("MEC1foodCO2") + GETVALUE("MEC1purchasesCO2"); 
    my $MEC1total3 = $MEC1total1 - $MEC1total2;
    my $MEC1total4 = $MEC1total3 * 2200;
    $MEC1totals = $MEC1total4;
}
my $intVarTotal = $MEC1totals;
my $roundedTotal = sprintf("%.4f", $intVarTotal);
if($intVarTotal > 0)
{
    SETVALUE("MEC1total", $roundedTotal);
}

#Bonuses
if(GETVALUE("Section6") eq "done")
{
    $MEC1bonuses = GETVALUE("MEC1S1bonus") + GETVALUE("MEC1S2bonus");
}
my $intVarBonus = $MEC1bonuses;
my $roundedBonus = sprintf("%.4f", $intVarBonus);
if($intVarBonus > 0)
{

    SETVALUE("MEC1bonus", $roundedBonus);
}

#Total points
if(GETVALUE("Section6") eq "done")
{
    $MEC1points = GETVALUE("MEC1bonus") + GETVALUE("MEC1total");
}
my $intVarPoints = $MEC1points;
my $roundedPoints = sprintf("%.4f", $intVarPoints);
if($intVarPoints > 0)
{

    SETVALUE("MEC1points", $roundedPoints);
}

#Total Exercise points
if(GETVALUE("Section6") eq "done")
{
    $MEC1totalExercise = (GETVALUE("MEC1bonus") + GETVALUE("MEC1total")) * GETVALUE("Multiplier");
}
my $intVarTE = $MEC1totalExercise;
my $roundedTEPoints = sprintf("%.4f", $intVarTE);
if($intVarTE > 0)
{

    SETVALUE("MEC1TEpoints", $roundedTEPoints);
}

End Unverified %]








I'm not sure the cause of the problem.  One quick way to debug unverified Perl is to add temporary returns throughout the code.  What happens if you put this code between lines 7 and 8?

return $baselineQ1;


What about this line between lines 12 and 13?

return $MEC1total1;


Are these temporary returns printing out the result you would expect?
the return $basleineQ1 is returning the right value.

return $MEC1total1 isn't returning anything - but would it since it's inside the statement? If it should than we may be getting closer to the issue.
You can do returns inside ifs, fors, etc.

My guess is that the code isn't going into the if block at all.  Try putting this at the beginning of the if:

return 'Hi';


If "Hi" doesn't show up at runtime, then your Section6 pass-in has a problem.  You can print it before the if statement like this:

return '~' . GETVALUE("Section6") . '~';


(Note: I added tildes to the each end of the printed out string so you can more easily see if the problem is whitespace in the pass-in field.  If that code prints out something like "~done ~", then your pass-in field has whitespace that the Perl if statement isn't expecting.)
Neither of these lines showed up when I ran the page. :/
Can you email me a .ssi showing the problem?
Yes, where do I find your email?
zachary @ sawtoothsoftware.com

1 Answer

0 votes
Just a small point: Is the pass-in field defined as "BaselineQ1" or "baselineQ1"? You must reference these variables as you define them otherwise you Perl script will fail.

So should it be ...

my $baselineQ1 = GETVALUE("BaselineQ1");
OR
my $baselineQ1 = GETVALUE("baselineQ1");
answered Mar 7, 2017 by Paul Moon Platinum (66,070 points)
The baseline reference is correct case.
...