Have an idea?

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

Ah, I knew I was running into a local/global variable issue, but my lack of PERL and JS knowledge kept me from working it out. This resolved the issue. I had changed the code some since I posted this, but was still applicable even where I was at in the end. Thanks so much!

While that code worked, I tried to add in a couple more lines of math for some additional grid entries and now it doesn't seem to be picking it up. These two new grid entries are numeric entry boxes only. Any ideas where my issue is?

The likely cause of the problem is now apparent: your S3 and S4 code is only going to run if the respondent has an additional car. You probably want the S3 and S4 "if" codes to run outside the additional car "if."

Thank you! Sorry for all the newb questions here, should have caught that. One last issue. I created a pass in field for a variable called "transportationCO2" and then added this to the end of the code:

You're welcome, and no worries. This forum is open to all levels of programming experience. For your code, two things stand out:

First, you need a semicolon at the end of your SETVALUE line of code. JavaScript lets users get away with not using semicolons in most cases, but most other programming languages are much less forgiving.

Second, you need to switch lines 2 and 4. A "return" statement ends a block of code. Code that appears after the "return" will not be ran.

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

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.