From: Brian Candler Date: 2008-12-03T06:45:25+09:00 Subject: Re: How do you read text file by creating a Hash in a script? 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" -- Posted via http://www.ruby-forum.com/.