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


#1

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


#2

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


#3

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.


#4

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”


#5

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