0

I'm having a bit of trouble adding a couple variables together. Nothing I've tried has fixed the problem, and I'm doing some stuff I've never done before which is making me wonder what is actually causing the problem. I have a .csv file which I need to read through to find the line I want, pull some variables from there, add them together, and display all this on a page.

Here's my code:

<script src="jquery_min.js" type="text/javascript"></script>
<script src="jquery.csv-0.71.js" type="text/javascript"></script>
<script>

jQuery.get('fileName.csv', function(data) {
var csv = $.csv.toArrays(data);
var index, value, result, var1, var2, var3;
for (index=0; index < csv.length; ++index) {
    value = csv[index];

    if (value[3] === "1234") {
        window.var1 = parseFloat(value[5]);
        window.var2= parseFloat(value[6]);
        window.var3= var1 + var2;
        alert(var3);
        result = var3;
        break;
    }
}
});

</script>

and here's a sample line from the .csv file:

"NEW YORK","A123","SMITH INC","1234","JOHN SMITH",4112237,5279,,"Y"

I'm writing to the page with this:

<script>
$('td#id1').html(var1);
$('td#id2').html(var2);
$('td.class1').html(var3);
</script>

The alert pops back with "undefined" while var1 and var 2 are written to the page correctly, as 4112237 and 5279 respectively. Var3 writes to the page as NaN. At this point I've tried too many fixes to recall. Any help would be appreciated.

MNRSullivan
  • 587
  • 7
  • 18

2 Answers2

3

Remove those window. things! That will access properties of the global object (aka global variables), but your var1, var2, var3 are locally declared - and therefore still undefined when you access them (in the alert call or result assignment)!

jQuery.get('fileName.csv', function(data) {
    var csv = $.csv.toArrays(data);
    var index, value, result, var1, var2, var3;
    for (index=0; index < csv.length; ++index) {
        value = csv[index];

        if (value[3] === "1234") {
            var1 = parseFloat(value[5]);
            var2 = parseFloat(value[6]);
            var3 = var1 + var2;
            alert(var3);
            result = var3;
            break;
        }
    }

    $('td#id1').html(var1);
    $('td#id2').html(var2);
    $('td.class1').html(var3);
});

If those global variables were supposed to be read by the next script, have a look at How do I return the response from an asynchronous call? and maybe at Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thank you for the prompt response. Your solution will ultimately be what I use, but I also discovered that my code worked if I added window. to each variable in the addition line. So it read window.var3 = window.var1 + window.var2; – MNRSullivan Jul 07 '14 at 21:20
  • Yes, but global variables are to be avoieded. – Bergi Jul 07 '14 at 21:39
0

Your problem is that you are treating "value" like an array with an index. Value only contains the value from csv[index]. Refactor your code like this:

jQuery.get('fileName.csv', function(data) {
    var csv = $.csv.toArrays(data);
    var index, value, result, var1, var2, var3;
    for (index=0; index < csv.length; ++index) {
        value = csv[index];

        if (value === "1234") {
            var1 = parseFloat(csv[5]);
            var2 = parseFloat(csv[6]);
            var3 = var1 + var2;
            alert(var3);
            result = var3;
            break;
        }
    }
});
Wes King
  • 627
  • 4
  • 7