1

I have a function which is called when a file is needed to be read in a folder. In this case since i have 3 files in that folder, it is called 3 times consecutively. I need to save all files info into array mapped_data2 like that:

mapped_data2[0] = inner_data1 //first file info, 
mapped_data2[1] = inner_data2 //second file info etc. 

However using my code i am having just the first files information 3 times. I am a bit confused with global variables, if you can point out the problem, i would appreciate it.

here is the code:

var mapped_data = [];
var mapped_data2 = [];                   
function o(msg) {                    
     if (msg.data) {
         var inner_data = [];
         var lines = msg.data.split('\n'); //read lines of a file
         for (var i = 2; i < lines.length; i++) {
              if (lines[i].length > 0) {
                  .. //do same data format here
                  inner_data.push([son, vactual, voutput]);    
              }  
         } 
     mapped_data = inner_data;   
     } 
     else {
         if (msg.topic == "/somefolder/somefolder") {
                  for (var i = 0; i < msg.args.length; i++) {
                  var filename = msg.args[i];
                  aw.get(filename);
                  }
         } 
     }
}

function de() { //where i wanted to use these files info
    for (var i = 0; i < 3; i++) {
         mapped_data2[i] = { key: "Group" + (i + 1), values: mapped_data };
    }

    var datam = mapped_data2;
    var den = JSON.stringify(datam);
    document.write(den);
};

function init() {
                  ..//create new client called aw for the server application and start it;
                  ..//if the connection is established:  
                  aw.onmessage = o;
                  aw.get("/somefolder/somefolder"); // request list of files in a folder                         
                 };
//when html page is being onload, call the functions init() and de()
3
  • 2
    Where are you calling the function "o(msg)"? I don't see where the files are being read for processing. Commented May 14, 2013 at 10:32
  • As already pointed out, you should show the code for the reading part. I guess it's a series of asynchronous GET requests (and you're calling the function repeatedly before they end). Commented May 14, 2013 at 10:48
  • @Aioros Hi, I added those parts, thanks! Commented May 14, 2013 at 11:02

1 Answer 1

1
var mapped_data2 = [];                   

function o(msg) {
     var mapped_data = []; // this doesn't need to be global
     if (msg.data) {
         var inner_data = [];
         ...             
         mapped_data = inner_data;   
     } else {
         ...
     }

     mapped_data2.push({ key: "Group" + mapped_data2.length + 1, values: mapped_data };)
     // do more stuff as in den()
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you reply. I couldn't make it work here, since in de() function i cannot call o() to be values. It requires parameter and msg is not defined inside that function.
@sgizm See updated answer; you can move the contents of de() in o().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.