1

I am very new to JScript, and windows scripting in general, and I have some data which I have read from a file and converted to a string, I want to split this data into two data arrays so they can be used separately. The data looks like this when I output from the file, showing the two parts which are deliminated by a newline character. If I split the data using (" ") it correctly separates each number, however I want to do this after separating the two sets of data first.

5.000000 9.000000 3.000000 7.000000 1.000000 4.000000 6.000000 2.000000 8.000000 0.000000 
2464.000000 2464.000000 616.000000 2464.000000 154.000000 1232.000000 2464.000000 308.000000 2464.000000 77.000000 

When I run code to split the two parts I receive this result

res 0 = 5.000000 9.000000 3.000000 7.000000 1.000000 4.000000 6.000000 2.000000 8.000000 0.000000 ,2464.000000 2464.000000 616.000000 2464.000000 154.000000 1232.000000 2464.000000 308.000000 2464.000000 77.000000 
res 1 = undefined

This is the code I used to generate the above outputs

//Outputs 1
for( let i=0; i<data.length; i++) {
  println(data[i]);
  }
data = data + ''; //Convert Data from object to String
var res = data.split("\n")
//Outputs 2
console.log("res 0 = " + res[0]);
console.log("res 1 = " + res[1]);

This is the result I want

res 0 = 5.000000 9.000000 3.000000 7.000000 1.000000 4.000000 6.000000 2.000000 8.000000 0.000000
res 1 = 2464.000000 2464.000000 616.000000 2464.000000 154.000000 1232.000000 2464.000000 308.000000 2464.000000 77.000000

Edit: Code that reads the data

function dataRead(fname) {
  console.log("Reading sequence...");
  var fs = WScript.CreateObject("Scripting.FileSystemObject");
  var lines = [];
  var stream = fs.OpenTextFile(fname);
  while(!stream.AtEndOfStream) {
    lines.push(stream.ReadLine());
  }

  stream.Close();

  return lines;
}

Edit: Matlab Code that writes the data

data = [T.seq; T.duration_bits];
    newfile = [dir '\' name '.txt']
    fp = fopen(newfile, 'w');
    fprintf(fp,'%f ', data(1,:));
    fprintf(fp,'\n');
    fprintf(fp,'%f ', data(2,:));
    fclose(fp);

(object to string was from What is causing the error `string.split is not a function`?)

Daniel
  • 11
  • 3
  • Have you checked if ```data``` contains ```\n```? Where is the code that reads your file? – Melon Feb 24 '20 at 10:29
  • I have added the code that reads the file, the data is generated using MATLAB, and I use fprintf to specifically add '\n' to the data after that line – Daniel Feb 24 '20 at 10:31
  • Okay, so now I see that you are returning ```lines``` from ```dataRead```. My assumption is that you size of lines is ```2```. Am I correct? – Melon Feb 24 '20 at 10:35
  • Yes that is correct – Daniel Feb 24 '20 at 10:36
  • So why don't you split (by spaces) ```lines[0]``` into ```res0``` and ```lines[1]``` into ```res1```? – Melon Feb 24 '20 at 10:41
  • I would like a way to do this more generally as `data` won't always be 2 lines, and I am using this example to try and find a way to handle arbitrarily large sets of data – Daniel Feb 24 '20 at 10:43
  • Ah yes, okay, I see what you mean, I am very dumb – Daniel Feb 24 '20 at 11:12
  • Also, please remember that functions that read lines from files across many languages remove the ```\n```. – Melon Feb 24 '20 at 13:04

1 Answers1

0

Found answer, thank you, I have posted the full code that gave me the answer I wanted, along with its outputs below

var data = import_sequence(path_func, path, file, ext);
for( let i=0; i<data.length; i++) {
  println(data[i]);
  }
var seq = data[0];
seq = seq.split(" ").map(Number);
seq.pop();
console.log("seq = "+ seq);

var dur_bits = data[1];
dur_bits = dur_bits.split(" ").map(Number);
dur_bits.pop();
console.log("dur_bits = " + dur_bits);
}
console.log("dur = " + dur);

Output

Reading sequence...
Data Line0 = 5.000000 9.000000 3.000000 7.000000 1.000000 4.000000 6.000000 2.000000 8.000000 0.000000 
Data Line1 = 2464.000000 2464.000000 616.000000 2464.000000 154.000000 1232.000000 2464.000000 308.000000 2464.000000 77.000000 
seq = 5,9,3,7,1,4,6,2,8,0
dur_bits = 2464,2464,616,2464,154,1232,2464,308,2464,77
Daniel
  • 11
  • 3