Tips for parsing a tsp library file with JavaScript

Looking for assistance with a tsp library file in JavaScript. The specific file, named file1.tsp, contains the following data:

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

The goal is to extract the numbers and organize them into arrays as follows:

node =[1,2,3,4,...]
x=[288,288,...]
y=[149,129,...]

If anyone is able to provide guidance on how to achieve this, your help would be greatly appreciated. Thanks in advance!

Answer №1

Perhaps you could try something along these lines:

const fileData = `
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 linesWithDigits = fileData.match(/^ *\d+ +\d+ +\d+$/gm);

const nodeArr = [];
const xArr = [];
const yArr = [];

for (const line of linesWithDigits) {
  const digits = line.trim().split(/ +/);
  nodeArr.push(Number(digits[0]));
  xArr.push(Number(digits[1]));
  yArr.push(Number(digits[2]));
}

console.log(nodeArr);
console.log(xArr);
console.log(yArr);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to efficiently display elements in a grid in real-time

Attempting to design a layout where each row consists of 3 cards using Bootstrap's Grid layout for responsiveness. The challenge lies in the fact that the card data is stored in JSON format, and the length of the JSON Object array may vary, leading t ...

The Vue2 @click event does not function properly when using v-html within a different component

I currently have a sign up form that includes an Alert component for displaying any errors. One of the conditions may prompt a message saying "You already have an account, click here to log in". The error messages are passed as props to the "Alert" compon ...

Can you explain how this particular web service uses JSON to display slide images?

{ "gallery_images": [ {"img":"http://www.jcwholesale.co.uk/slider_img/big/1_1433518577.jpg"}, {"img":"http://www.jcwholesale.co.uk/slider_img/big/1_1433519494.jpg"}, {"img":"http://www.jcwholesale.co.uk/slider_img ...

The height of the div element is automatically adjusted to zero

I am dealing with a simple layout where I have a main div that floats to the left. Within this main div, I nest other divs using the clear both style. Below is a simplified version of my setup: <div id="wrapper" class="floatLeft"> <div id="ma ...

A guide on how to reset Orbit Controls once the target has been defined

After clicking the mouse, I successfully set a new target for my THREE.OrbitControls and it works perfectly. However, once the camera pans to the new location, I lose all mouse interaction. I suspect that I may have broken the controls when I made the came ...

Tips for extracting title and image from someone else's blog posts to share on your own website

Currently, I am in the process of creating a website where I can showcase my personally curated content. I have been struggling to figure out how to seamlessly integrate this content into my site without relying on an API. My initial idea was to manually ...

Why doesn't the element I'm clicking return as expected?

Below is the code provided <!DOCTYPE html> <html> <body> <p id="demo">Demo Element</p> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script> $('#demo' ...

Can the parameters in a .slice() be customized within a v-for loop?

I am currently working with Laravel 8 and using blade syntax. The following code snippet is from a Vue component I created: <li class="w-3/12 md:w-auto pt-0 md:px-4 md:pt-2 md:pb-0 list-none relative" v-if="comic.item_type === 'b&ap ...

Is it possible to swap images by clicking on them?

Let's say I'm working with 3 images. Image A, B and C. A is the main image initially. If I click on image B, it will become the main image and image A will take its place in the old position. The same condition applies when B is the main image a ...

The Cy.visit() function in Cypress times out and bypasses tests specifically when navigating to a VueJS web application

Having trouble with the Cypress cy.visit() function timing out and aborting tests on a VueJS Web Application? It seems to work fine when opening other non-VueJS sites. Below is my basic configuration setup: [package.json] "dependencies": { "cypres ...

I am facing a dilemma where React Native seems to be disregarding all the container styles I

Here is the code snippet I am working on: import React from 'react'; import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; export default class ShortcutsHome extends React.Component { render() { return ( ...

Animating Divs with jQuery to Expand their Size

I am currently designing a services page for my portfolio website. The layout consists of three columns, with the central column containing a large box and the left and right columns each containing three smaller boxes. These smaller boxes function as clic ...

Manipulate images in real-time and insert custom text using JavaScript/jQuery

Within my possession is an image depicted above. My objective revolves around dynamically altering the values present at the occurrences of L, A, and B; to achieve this, I must eliminate or conceal L, A, and B while substituting them with numerical equiv ...

The Angular performance may be impacted by the constant recalculation of ngStyle when clicking on various input fields

I am facing a frustrating performance issue. Within my component, I have implemented ngStyle and I would rather not rewrite it. However, every time I interact with random input fields on the same page (even from another component), the ngStyle recalculate ...

Organize array elements based on their values - using javascript

I am currently exploring the most effective approach to divide an array into multiple arrays based on specific operations performed on the values within the array. Imagine I have an array like this: var arr = [100, 200, 300, 500, 600, 700, 1000, 1100, 12 ...

Loop through to display response information

After submitting data using jQuery's .ajax() method, I receive a response back with some information that needs to be displayed on my webpage. For instance, this is an example of the response: { "res": 1, "item": [ {"id":"1","desc":"blue ...

Senecajs responded with a result that was neither an object nor an array, displaying a Promise

Seeking guidance on incorporating promises into my Seneca modules. Firstly, there is the server.js file that exposes a route: var express = require('express'); var app = express(); var Promise = require('bluebird'); var seneca = requ ...

jquery surprise confirmation/prompt substitution

I've been working on creating a library code for alert, confirm, and prompt replacements using jQuery and Impromptu. My goal is to be able to call these functions in a similar way: For example: var userInput = jConfirm("Are you sure?"); alert(userIn ...

Manipulate object attributes in PHP5 Laravel by replacing any that match a certain value

Currently, I am in the process of constructing a website using Laravel 5 framework, and I have encountered a slight issue. After extracting an array of objects from the database using the Eloquent model, I have passed it to a view. The structure of the a ...

Mobile devices experiencing issues with mouse events

My 360 panorama image works perfectly on desktop, but I'm having trouble getting the mouse events to work on mobile phones. How can I resolve this issue? // listeners document.addEventListener("mousedown", onDocumentMouseDown, false); document.addEv ...