Someone gave to me a code snippet to load float values from an ASCII file (here Positions.dat) and to set these values into an array. This code uses jQuery:
$.get("/Positions.dat", function(content) {
var array = content.split(/\n/).filter(function(line) {
return line.trim().length > 0;
}).map(function(line) {
return line.trim().split(/\s+/).map(function(word) {
return +parseFloat(word);
});
});
console.log(array);
alert(array);
});
The file Positions.dat
is on server side and contains 3 columns of float values.
I am not a Javascript expert and I would like to understand better this code snippet.
I think it firstly split all the lines with '\n' delimiter
, then only select the lines with a number of elements > 0 per line
(select only non-empty lines with trim
), then for each line split the 3 words and convert each one to float.
The syntax is quite complex for me, anyone could give more precisions on the nested functions used ( map
for line
and word
) ?
Secondly, I try to include this code into another script to put array values into another array :
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$.get("/Positions.dat", function(content) {
var array = content.split(/\n/).filter(function(line) {
return line.trim().length > 0;
}).map(function(line) {
return line.trim().split(/\s+/).map(function(word) {
return +parseFloat(word);
});
});
});
for(var p = 0; p < 10; p++) {
var pX = array[p*3];
var pY = array[p*3+1];
var pZ = array[p*3+2];
}
</script>
But I get the following error on console :
ReferenceError: array is not defined
Maybe array is a local variable
above, so I try to put it global
but I get the same error.
What can I try next?