Well,
your primary problem is that you write both elements of the
arrays on separat lines. When reading you will read more
lines than you expect (twice as much to be correct).
So an easy fix would be to write using
fileOut.puts histHash[i].join(',')
so both values are on the same line.
If you read them in you have to split the line to get two
distinct values again.
fileIn.each_line{|line|histHash[i]= line.split(',').map{|s| s.to_i}}
that said, I would recommend having a close look at yaml.
cheers
Simon
···
-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of David Bailey
Sent: Thursday, April 20, 2006 11:01 AM
To: ruby-talk ML
Subject: Write Out Then Read In Hash of Two-element ArraysKind All,
I wonder if one of you knowledgeble folks can steer me in he right
direction:I'm trying to get a hash I've created read back in after
after writing
it out to a file, so that it is exactly the same afterward. The hash
has zero-based integer keys and two-element array values.After I create and populate the hash:
Length of histHash after creation = 5When I write out the hash, the output to STDOUT looks like this:
Write histHash[0] = 101
Write histHash[1] = 112
Write histHash[2] = 123
Write histHash[3] = 134
Write histHash[4] = 145
5 histHash lines written
histHash written = 01011112212331344145After I delete the elements of the hash:
Length of histHash after deletion = 0But when I read it back in, it looks like this:
Read histHash[0] = 10; line = 10
Read histHash[1] = 1; line = 1
Read histHash[2] = 11; line = 11
Read histHash[3] = 2; line = 2
Read histHash[4] = 12; line = 12
Read histHash[5] = 3; line = 3
Read histHash[6] = 13; line = 13
Read histHash[7] = 4; line = 4
Read histHash[8] = 14; line = 14
Read histHash[9] = 5; line = 5
10 histHash lines read
histHash read = 5301061311742118143295412
Length of histHash after read = 10As you can see, it's length is five lines when I initially
write it to
the file, but ten lines when I read it back in. I've been
pawing though
the docs, but I can't figure out what I'm doing wrong. Any
help will be
greatly appreciated.Here is the errant code:
# write out and then read in hash of two-element arrays
# create and populate the hash
histHash = Hash.new
histHash[0] = [10, 1]
histHash[1] = [11, 2]
histHash[2] = [12, 3]
histHash[3] = [13, 4]
histHash[4] = [14, 5]
puts
puts "Length of histHash after creation = #{histHash.length}"
puts# write the hash to the file
fileOut = File.new("savedHash.sdo", "w")
histHash.length.times do |i|
fileOut.puts(histHash[i])
puts "Write histHash[#{i}] = #{histHash[i]}"
end
fileOut.closeputs "#{histHash.length} histHash lines written"
puts "histHash written = #{histHash}"
puts# Delete the hist hash elements
histHash.length.times do |i|
histHash.delete(i)
end
puts "Length of histHash after deletion = #{histHash.length}"
puts# load (read) the saved file
fileIn = File.new("savedHash.sdo", "r")
i = 0
fileIn.each_line{|line|histHash[i]=[line.to_i]; puts "Read
histHash[#{i}] = #{histHash[i]}; line = #{line}";i+=1}
fileIn.close
puts "#{histHash.length} histHash lines read"
puts "histHash read = #{histHash}"
puts "Length of histHash after read = #{histHash.length}"
puts--
Posted via http://www.ruby-forum.com/.