I have a tsp library file, that I want to read in javaScript. The file is below:
file1.tsp
NAME: a280
COMMENT: drilling problem (Ludwig)
TYPE: TSP
DIMENSION: 280
EDGE_WEIGHT_TYPE: EUC_2D
NODE_COORD_SECTION
1 288 149
2 288 129
3 270 133
4 256 141
5 256 157
6 246 157
7 236 169
8 228 169
9 228 161
10 220 169
11 212 169
12 204 169
13 196 169
14 188 169
15 196 161
16 188 145
17 172 145
18 164 145
EOF
I only want to read the numbers, and store them in an array like this;
node =[1,2,3,4,....]
x=[288,288....]
y=[149,129...]
Can anyone help? Thanks!
Maybe something like this:
const fileContent = `
NAME: a280
COMMENT: drilling problem (Ludwig)
TYPE: TSP
DIMENSION: 280
EDGE_WEIGHT_TYPE: EUC_2D
NODE_COORD_SECTION
1 288 149
2 288 129
3 270 133
4 256 141
5 256 157
6 246 157
7 236 169
8 228 169
9 228 161
10 220 169
11 212 169
12 204 169
13 196 169
14 188 169
15 196 161
16 188 145
17 172 145
18 164 145
EOF
`;
const linesWithNumbers = fileContent.match(/^ *\d+ +\d+ +\d+$/gm);
const node =[];
const x = [];
const y = [];
for (const line of linesWithNumbers) {
const numbers = line.trim().split(/ +/);
node.push(Number(numbers[0]));
x.push(Number(numbers[1]));
y.push(Number(numbers[2]));
}
console.log(node);
console.log(x);
console.log(y);