Write Out Then Read In Hash of Two-element Arrays

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 Arrays

Kind 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 = 5

When 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 = 01011112212331344145

After I delete the elements of the hash:
Length of histHash after deletion = 0

But 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 = 10

As 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.close

puts "#{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/\.

Kroeger, Simon (ext) wrote:

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

Simon,

Thank you. Your suggestion, of course, worked. Why I oddly thought
Ruby should split the hash elements into separate lines, but not the its
array elements, I'll never know! After being up all night, I guess my
brain just stopped working.

I've seen the acronym yaml around a few times, but short a guessing that
it means yet another meta language I do not know what it actually does,
but I will take a look at it as you suggest.

Again, thank you. I'm awake now! (:>)

David

···

--
Posted via http://www.ruby-forum.com/\.

it's a serialization format, not a metalanguage. the thing is it's a
__readable__ one:

     harp:~ > cat a.rb
     require 'yaml'

     hash = {
       "a" => 0,
       "b" => 1,
       "c" => 2,
     }

     open('hash','w'){|f| f.write hash.to_yaml}
     open('hash'){|f| print f.read}
     hash = open('hash'){|f| YAML::load f}
     p hash

     harp:~ > ruby a.rb

···

On Fri, 21 Apr 2006, David Bailey wrote:

Thank you. Your suggestion, of course, worked. Why I oddly thought Ruby
should split the hash elements into separate lines, but not the its array
elements, I'll never know! After being up all night, I guess my brain just
stopped working.

I've seen the acronym yaml around a few times, but short a guessing that it
means yet another meta language I do not know what it actually does, but I
will take a look at it as you suggest.

Again, thank you. I'm awake now! (:>)

David

     ---
     a: 0
     b: 1
     c: 2
     {"a"=>0, "b"=>1, "c"=>2}

regards.

-a
--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

David Bailey wrote:

Kroeger, Simon (ext) wrote:

that said, I would recommend having a close look at yaml.

cheers

Simon

Simon,

Thank you. Your suggestion, of course, worked. Why I oddly thought
Ruby should split the hash elements into separate lines, but not the its
array elements, I'll never know! After being up all night, I guess my
brain just stopped working.

I've seen the acronym yaml around a few times, but short a guessing that
it means yet another meta language I do not know what it actually does,
but I will take a look at it as you suggest.

Again, thank you. I'm awake now! (:>)

David

Uh, I stand corrected. I do actually recall reading about yaml. It was
covered in Programming Ruby by Dave Thomas and the pragmatic
programmers. I guess I just forgot. God, I hate when that happens!
(:>)

···

--
Posted via http://www.ruby-forum.com/\.

unknown wrote:

···

On Fri, 21 Apr 2006, David Bailey wrote:

David

it's a serialization format, not a metalanguage. the thing is it's a
__readable__ one:

     harp:~ > cat a.rb
     require 'yaml'

     hash = {
       "a" => 0,
       "b" => 1,
       "c" => 2,
     }

     open('hash','w'){|f| f.write hash.to_yaml}
     open('hash'){|f| print f.read}
     hash = open('hash'){|f| YAML::load f}
     p hash

     harp:~ > ruby a.rb
     ---
     a: 0
     b: 1
     c: 2
     {"a"=>0, "b"=>1, "c"=>2}

regards.

-a

Yes, thank you for jogging my memory! Now that yo mention it, I do
recall reading about yaml in the pickaxe book. It slipped off the edge
of my mind, I guess. Geeze, I dislike that forgetfulness thing ...
(:>)

--
Posted via http://www.ruby-forum.com/\.