0

okay i have a function within my node.js script using the event stream module.

i can check the line data with the simple script i have below but i want to be able to use each line aswell. example of what i want

line(0)  --- first line of stdout 
line(1)  --- second line of stdout
ect, ect

my script i am using at the moment

function pinstripe() {
    es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
        console.log(line);
        if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');


        }

        if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');


        }

    })
);   
}

then i call it like so

pinstripe();

this works like expected but how do i make the function so i can use it like so ..

function pinstripe() {
    es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
        console.log(line);
        if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');
        child.stdin.write(line(0));   //i want to refrence
        child.stdin.write(line(1));   //each line like so 

        }

        if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');
        child.stdin.write(line(2));   //and here


        }

    })
);   
}

obviously this does not work but how can i make it so it does work

user3407899
  • 139
  • 4
  • 16
  • You need to save all lines somewhere (in an Array for instance) in order for referencing them later. This makes streaming useless however... Unless you save a limited number of lines (ex. according to a specific pattern.) – fardjad Apr 03 '14 at 20:45
  • writing it to an array would be perfect is there a way i can push each line into an array ? – user3407899 Apr 03 '14 at 20:52
  • the main lines i want at the minute are line 1 line 2 and line 3 – user3407899 Apr 03 '14 at 20:53

1 Answers1

0

Try this:

function pinstripe() {
  var index = 0,
      lines = [];

  es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
      console.log(line);
      if (index <= 2) {
        lines[index++] = line;
      }
      if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');
        child.stdin.write(lines[0]);
        child.stdin.write(lines[1]);
        // set index = 0 to start over
      }
      if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');
        child.stdin.write(lines[2]);
        // set index = 0 to start over
      }
    })
  );
}
fardjad
  • 20,031
  • 6
  • 53
  • 68
  • unfortunatly this does not work i do not get any of the output from [0][1] or [3] but everything else is there – user3407899 Apr 03 '14 at 21:07
  • @user3407899 Can you please post the contents of `lines`? (just put a `console.log(lines)` before `child.stdin.write(lines[0]);`) – fardjad Apr 03 '14 at 21:22