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

  • Home
  • SEARCH
  • 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 319493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:39:59+00:00 2026-05-12T08:39:59+00:00

So I have a file in the form of: Key1 Value1 Key2 Value2 Key3

  • 0

So I have a file in the form of:

Key1   Value1
Key2   Value2
Key3   Value3

seperated by a tab. My question is how do I open this file and put it into a hash? I have tried to do:

 fp = File.open(file_path)

 fp.each do |line|
   value = line.chomp.split("\t")
   hash = Hash[*value.flatten]
 end

But at the end of this loop the @datafile hash only contains the latest entry…I kinda want it all…..

  • 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-12T08:39:59+00:00Added an answer on May 12, 2026 at 8:39 am

    hash[key] = value to add a new key-value pair. hash.update(otherhash) to add the key-value-pairs from otherhash to hash.

    If you do hash = foo, you reassign hash, losing the old contents.

    So for your case, you can do:

    hash = {}
    File.open(file_path) do |fp|
      fp.each do |line|
        key, value = line.chomp.split("\t")
        hash[key] = value
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file in the form of Java properties file: param1=value1 param2=value2 ......
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I have one windows form file that each time I open it in Visual
I have a file containing lots of data put in a form similar to
I have CSV file with sample data in this form : 220 30 255
I have a file upload form how can I retain this file when there
I have this case : I have a file named form.php and it contains
I have to develop a file server service that transfer file form a specified
I have file with name contact.php, inside I have form: <form method=post name=kontakt action=send_mail.php>
We have got an extjs 3.1.1 form with file upload field ( http://www.extjs.com/deploy/dev/examples/form/file-upload.html from

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.