0

i have a grep function that i am using to seperate some data out.

I ran into an issue, instead of outputting the data to the console, i need it to store it to a variable.

for example, here is my actual function.

function funGrep(cmd,callback,search,args){
    exec(cmd,function(err,stdout){
        if(!stdout)
            return;
        var lines = stdout.toString().split(EOL);
        var re = new RegExp(search,args);
        for(var line in lines){
            var results = lines[line].match(re);
            if(results){
                for(var i = 0; i < results.length; i++){
                    callback(results[i]);
                }
            }
        }
    });
}

and here is my actual calling of the function into play.

funGrep("ping -n 3 google.com",console.log,"time=[0-9\.]+ ?ms");

instead of logging the output to the console, how can i just assign it to a variable like output.

thank you!

JAAulde
  • 19,250
  • 5
  • 52
  • 63
Christopher Allen
  • 7,769
  • 5
  • 20
  • 35
  • So, instead of `console.log` pass a function in which the parameter is available as a variable? What is the problem? You [cannot simply `return` it](http://stackoverflow.com/q/23667086/1048572) of course. – Bergi Jun 16 '14 at 05:42
  • Pass a function that does that as the callback. Just be aware that it's running asynchronously. – Scimonster Jun 16 '14 at 05:42
  • i just need to be able to access the data instead of logging it to a console. Is there any way to do this, with or without modifying the actual function? I apoligize in advance if i seem like a noob because i am at node – Christopher Allen Jun 16 '14 at 05:47
  • i dont understand what you mean @Scimonster, how could i call a function inside of the pipe? – Christopher Allen Jun 16 '14 at 05:53
  • Instead of passing console.log, pass a different function taking one parameter and that one argument will be the data. – Scimonster Jun 16 '14 at 05:55

2 Answers2

0

All you should have to do is create your own callback function that does whatever you need it to do with your data/results. It would look something like this:

function theCallback (data) {
    ... do whatever you want with your data ...
}

And then instead of console.log, you would pass in this function as an argument.

funGrep("ping -n 3 google.com",theCallback,"time=[0-9\.]+ ?ms");
SeeMeCode
  • 1,415
  • 15
  • 19
0

You could you the callback to append the data to a variable, and modify the callback to notify the handler when the function has finished:

function funGrep(cmd,callback,search,args){
    exec(cmd,function(err,stdout){
        if(err){
            console.log(err);
            return;
        }
        if(!stdout)
            return;
        var lines = stdout.toString().split(EOL);
        var re = new RegExp(search,args);
        for(var line in lines){
            var results = lines[line].match(re);
            if(results){
                for(var i = 0; i < results.length; i++){
                    callback(results[i],false);
                }
            }
        }
        callback(null,true); //finsished
    });
}

var myData = [];
funGrep("ping -n 3 google.com",function(result,finished){if(!finished) myData.push(result); else goOn();},"time=[0-9\.]+ ms");

function goOn(){
    //funGrep finished
    console.log("Result: " + myData);
}
muffel
  • 7,004
  • 8
  • 57
  • 98