# Conditional PERL Math

I have a grid question with three columns, one is amount of miles driven, the second is the miles per gallon of the car, and the third is a drop down of fuel types. I need to run some math with those numbers, and a little different math depending on fuel type. Here is what I am working with...

[% Begin Unverified Perl

if(GETVALUE("S1_r1_c3")==1)
{
my \$vehicleOne =(GETVALUE("S1_r1_c1")/GETVALUE("S1_r1_c1"))*.01118+(GETVALUE("S1_r1_c1")*.0000563);
}
elsif(GETVALUE("S1_r1_c3")==2)
{
my \$vehicleOne =(GETVALUE("S1_r1_c1")/GETVALUE("S1_r1_c1"))*.01279+(GETVALUE("S1_r1_c1")*.0000563);
}
{
if(GETVALUE("S2_r1_c3")==1)
{
my \$vehicleTwo =(GETVALUE("S2_r1_c1")/GETVALUE("S2_r1_c1"))*.01118+(GETVALUE("S2_r1_c1")*.0000563);
}
elsif(GETVALUE("S2_r1_c3")==2)
{
my \$vehicleTwo =(GETVALUE("S2_r1_c1")/GETVALUE("S2_r1_c1"))*.01279+(GETVALUE("S2_r1_c1")*.0000563);
}
}

SETVALUE("vehicleCO2", \$vehicleOne+\$vehicleTwo);

End Unverified %]

+1 vote
I haven't ran the code myself, but the thing that jumps out to me is a scoping problem.  Here's code with that exemplifies this problem:

if (1 == 1) {
my \$x = 1;
}
SETVALUE("passin", \$x);

This code won't run.  Because the variable is initialized inside the "if," it exists only inside of the "if."  To fix that code, I would initialize the variable before the "if":

my \$x;
if (1 == 1) {
\$x = 1;
}
SETVALUE("passin", \$x);

So to fix the scope problem in your code, add this before the first "if":

my \$vehicleOne = 0;
my \$vehicleTwo = 0;

And then remove the "my" from inside the "if" and "elsif" codes.
answered Feb 23, 2017 by Platinum (98,000 points)
This project is killing me!!!
I know there is likely a scope issue here, but I cannot seem to track down why I can't set a value with the code below.

[% Begin Unverified Perl

my \$foodA = 0;
my \$foodB = 0;
my \$meat = 0;
my \$dairy = 0;
my \$fruit = 0;
my \$grains = 0;
my \$other = 0;
my \$dietType = 0;

if(GETVALUE("S3Q1_r1_c1") == 1)
{
if(GETVALUE("S3Q3_r1_c1") > 0)
{
\$meat = ((GETVALUE("S3Q3_r1_c1") / 100) * (GETVALUE("S3Q2_r1_c1") * .00000553 * 30));
}
if(GETVALUE("S3Q3_r2_c1") > 0)
{
\$dairy = ((GETVALUE("S3Q3_r2_c1") / 100) * (GETVALUE("S3Q2_r1_c1") * .000004 * 30));
}
if(GETVALUE("S3Q3_r3_c1") > 0)
{
\$fruit = ((GETVALUE("S3Q3_r3_c1") / 100) * (GETVALUE("S3Q2_r1_c1") * .00000335 * 30));
}
if(GETVALUE("S3Q3_r4_c1") > 0)
{
\$grains = ((GETVALUE("S3Q3_r4_c1") / 100) * (GETVALUE("S3Q2_r1_c1") * .00000145 * 30));
}
if(GETVALUE("S3Q3_r5_c1") > 0)
{
\$other = ((GETVALUE("S3Q3_r5_c1") / 100) * (GETVALUE("S3Q2_r1_c1") * .00000224 * 30));
}

\$foodA = \$meat + \$dairy + \$fruit + \$grains + \$other;
}

if(GETVALUE("S3Q1_r1_c1") == 2)
{
if(GETVALUE("S3Q4_r1_c1") == 1)
{
\$dietType = GETVALUE("S3Q2_r1_c1") * .00719 * 30;
}
if(GETVALUE("S3Q4_r1_c1") == 2)
{
\$dietType = GETVALUE("S3Q2_r1_c1") * .00563 * 30;
}
if(GETVALUE("S3Q4_r1_c1") == 3)
{
\$dietType = GETVALUE("S3Q2_r1_c1") * .00467 * 30;
}
if(GETVALUE("S3Q4_r1_c1") == 4)
{
\$dietType = GETVALUE("S3Q2_r1_c1") * .00391 * 30;
}
if(GETVALUE("S3Q4_r1_c1") == 5)
{
\$dietType = GETVALUE("S3Q2_r1_c1") * .00381 * 30;
}
if(GETVALUE("S3Q4_r1_c1") == 6)
{
\$dietType = GETVALUE("S3Q2_r1_c1") * .00289 * 30;
}
my \$foodB = \$dietType;
}

if("\$foodA" > 0)
{
SETVALUE("foodCO2", \$foodA);
}
elsif("\$foodB" > 0)
{
SETVALUE("foodCO2", \$foodB);
}

End Unverified %]
"\$foodB" is initialized twice, once on line 4 and again on line 64.  You should remove the "my" from the second case.

On a tangential note, you can probably remove the quotation marks on lines 68 and 72.  I don't think this is causing any bugs right now, but it's just a good practice.  You want to compare a number to another number on these lines; no need to convert the values into strings.
Also make sure all these variable do exist and are spelt correctly in your code. Perl is case sensitive - this includes variable and function names.
Bah, after all this the culprit was in the grid cell references. I have a couple grids that only contain one row and one column with a drop down. Instead of being S3Q4_r1_c1 it is just S3Q4_c1. Thank you both for the feedback.
Been there and done that before. Well done on getting across the finishing line.