Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > write hash/array to file

Reply
Thread Tools

write hash/array to file

 
 
zak
Guest
Posts: n/a
 
      08-11-2011
Hi,

i have a hash

h={"2011-01-01"=>[1,2,3], "2011-01-02"=>[2,3,4]}

how can i write this into a file having theis format:

2011-01-01 [1,2,3]
2011-01-02 [2,3,4]

I always end up with something like

2011-01-01 123
2011-01-02 234

Thanks!
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-11-2011
On 11.08.2011 11:40, zak wrote:
> i have a hash
>
> h={"2011-01-01"=>[1,2,3], "2011-01-02"=>[2,3,4]}
>
> how can i write this into a file having theis format:
>
> 2011-01-01 [1,2,3]
> 2011-01-02 [2,3,4]
>
> I always end up with something like
>
> 2011-01-01 123
> 2011-01-02 234


In this particular case you can do

irb(main):001:0> h={"2011-01-01"=>[1,2,3], "2011-01-02"=>[2,3,4]}
=> {"2011-01-01"=>[1, 2, 3], "2011-01-02"=>[2, 3, 4]}
irb(main):002:0> h.sort.each do |d,l| printf "%s\t%p\n", d, l end
2011-01-01 [1, 2, 3]
2011-01-02 [2, 3, 4]
=> [["2011-01-01", [1, 2, 3]], ["2011-01-02", [2, 3, 4]]]

I don't like that approach because it relies on the implementation of
#inspect. If that is changed your output will change, too. I would
rather do something like

irb(main):006:0> h.sort.each do |d,l| printf "%s\t[%s]\n", d, l.join(',
') end
2011-01-01 [1, 2, 3]
2011-01-02 [2, 3, 4]

where you have control over the output format.

And I suggest you make your keys instances of Date and convert them when
reading and writing. This gives you all the methods of Date for
manipulating them. You can convert them via

irb(main):021:0> Date.strptime '2011-02-01', '%Y-%m-%d'
=> #<Date: 2011-02-01 (4911187/2,0,2299161)>

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
zak
Guest
Posts: n/a
 
      08-11-2011
Very helpful, thank you!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file scad C++ 4 05-28-2009 08:47 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
is better to open, write, close file than open, write, append, close? Iulian Ilea Javascript 1 12-21-2006 04:32 PM
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM



Advertisments