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.

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 by Ron H.
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");
my $baselineQ1 = GETVALUE("baselineQ1");
answered Mar 7 by Paul Moon Platinum (55,975 points)
The baseline reference is correct case.