Forum: Ruby How do you read text file by creating a Hash in a script?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mmcolli00 M. (Guest)
on 2008-12-02 23:19
This is some example contents of my text file: logzT09.txt

00834 tue z0sdf
01230 wed z0sdf
34234 tue wet0f

How can I read this in another ruby script by using an Hash? I am
wanting to use certain lines/words to compare in another ruby script so
I need to be able to reference each word when needed. I am a beginner.
Please help if you can. Thanks. MC
Brian C. (Guest)
on 2008-12-02 23:26
Mmcolli00 Mom wrote:
> This is some example contents of my text file: logzT09.txt
>
> 00834 tue z0sdf
> 01230 wed z0sdf
> 34234 tue wet0f
>
> How can I read this in another ruby script by using an Hash?

Do you mean, how to read it into a Hash? Just read the file one line at
a time, splitting it into key and value as you desire, and then add that
as a new element into the Hash.

Methods you may find useful:

File.open
each_line   (for an IO object, yields a string for each line)
chomp       (for a String object, chop off the final newline)
split       (for a String object, split by default on spaces)

Hash methods:

h = {}          # create an empty Hash
h = Hash.new    # an alternative way of doing the same thing
h[key] = value  # create or overwrite an entry in the Hash
Mmcolli00 M. (Guest)
on 2008-12-02 23:35
yes I meant to read it into an Hash. Can you give me a snippet? I just
can't see it.

file = File.open("logzT09.txt")
rows = []
file.each_line do |line|
rows << line.split(" ")[2]
end
p rows

I know this is an array so it will not work.

I want to be able to say...if first row's data = '00834' then set a new
variable in another script of what data is availabe in that same row
'00834' following that number element. I just don't know how to use a
hash instead. The hash would be better since I can ,maybe, go by the
data availble instead of using a column or row id. I dont'know..I'll
keep at it. Thanks though.
Brian C. (Guest)
on 2008-12-02 23:51
Mmcolli00 Mom wrote:
> yes I meant to read it into an Hash. Can you give me a snippet?

I don't know how exactly you want to split the line into key and value.
There are three columns there.

> I just can't see it.
>
> file = File.open("logzT09.txt")
> rows = []
> file.each_line do |line|
> rows << line.split(" ")[2]
> end
> p rows
>
> I know this is an array so it will not work.

rows = {}          # will create a hash instead of array
...
key, val = line.chomp.split(" ",2)
rows[key] = val    # will add a value to the hash

This would split as ["00834", "tue z0sdf"]

You should end up with:

{
 "00834" => "tue z0sdf",
 "01230" => "wed z0sdf",
  ... etc
}

> I want to be able to say...if first row's data = '00834' then set a new
> variable in another script of what data is availabe in that same row
> '00834' following that number element.

Sorry, I'm unable to understand that sentence.

When this script runs, it cannot directly affect the behaviour of
another script. That would be a separate Ruby process with its own
workspace. (Unless you're doing something fancy like DRb for
inter-process communication, or writing data via a file or a pipe)

However, later on in *this* script you can make use of the hash you've
created.

  p rows["01230"]  # will print "wed z0sdf"
Mmcolli00 M. (Guest)
on 2008-12-03 00:02
Ok thanks. Yes I wanted to reference the row with with what is in the
first column. Your suggestion worked for that. My data will be dynamic
so I will have to figure out a way to call that new key everytime.
Thanks for helping. MC
This topic is locked and can not be replied to.