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

**URL:** <https://www.ruby-forum.com/t/re-write-out-then-read-in-hash-of-two-element-arrays/58298>\
**Category:** Ruby\
**Created:** [April 20, 2006, 11:37am UTC](https://www.ruby-forum.com/t/re-write-out-then-read-in-hash-of-two-element-arrays/58298 "2006-04-20T11:37:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [April 20, 2006, 11:37am UTC](https://www.ruby-forum.com/t/re-write-out-then-read-in-hash-of-two-element-arrays/58298/1 "2006-04-20T11:37:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [April 20, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/re-write-out-then-read-in-hash-of-two-element-arrays/58298/2 "2006-04-20T17:29:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [April 20, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/re-write-out-then-read-in-hash-of-two-element-arrays/58298/3 "2006-04-20T17:42:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [April 20, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/re-write-out-then-read-in-hash-of-two-element-arrays/58298/4 "2006-04-20T18:29:10Z")

</div>

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!  
(:\>)

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [April 20, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/re-write-out-then-read-in-hash-of-two-element-arrays/58298/5 "2006-04-20T18:33:46Z")

</div>

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 …  
(:\>)
