Re: Write Out Then Read In Hash of Two-element Arrays


#1

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


#2

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


#3

On Fri, 21 Apr 2006, David B. 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


#4

David B. 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 T. and the pragmatic
programmers. I guess I just forgot. God, I hate when that happens!
(:>)


#5

unknown wrote:

On Fri, 21 Apr 2006, David B. 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 …
(:>)