Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4125224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:50:29+00:00 2026-05-20T23:50:29+00:00

I am trying to parse a text file that is of the form: A

  • 0

I am trying to parse a text file that is of the form:

A B 2  
A G 6   
A C 99  
...  
B C 7  
B E 2  
B G 99

Now, these represent “nodes” and the “costs” between them to implement a shortest path algorithm.
I have decided to create a Node class that has 2 instance variables, a name and an array of hashes that are the neighboring nodes and costs to get there.

I am trying to figure out how to parse the file into a series of node objects from each group of lines.
The cost of 99 mean the nodes aren’t connected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:50:29+00:00Added an answer on May 20, 2026 at 11:50 pm

    First of all, you should also have a Graph class to manage the nodes and edges. Then something like this should get you started:

    g = Graph.new
    File.open('your_data_file').each do |line|
        n1, n2, wt = line.split(/\s+/)
        n1 = g.find_or_create_node(n1)
        n2 = g.find_or_create_node(n2)
        wt = wt.to_i
        g.add_edge(n1, n2, wt) if(wt != 99)
    end
    # g is now your fully populated graph.
    

    The graph, g, tracks the nodes it has so find_or_create_node can return the node if it already exists or create it and cache it if not. The add_edge method would connect n1 and n2 with an edge of weight wt.to_i.

    Your shortest path algorithm would, presumably, be a method call on the graph objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to a parse a file that has text in this format:
I am trying to parse out a text file that looks like the following:
I'm trying to parse a text file that has a heading and the body.
I am trying to parse a table in the form of a text file
I’m trying to parse text from a file that comes in a pseudo XML
I am trying to parse a text file that contains a variable number of
In Java, I am trying to parse an HTML file that contains complex text
I'm trying to parse a text file using parser combinators. I want to capture
I'm trying to parse text-based file attachments (txt, doc, etc...). However, I can't seem
I'm trying to parse and read a text file in Windows Phone 7. Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.