Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
How to read a tsp library file in javascript?

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!