From: Phrogz Date: 2007-12-05T00:50:20+09:00 Subject: Re: hash On Dec 4, 8:19 am, Phrogz wrote: > On Dec 4, 7:30 am, Johnathan Smith wrote: > > iv started writing a class which reads the data from a text file and > > counts the number of Tags > > > however im having a struggle as I want to split the tag and the data > > into a hash i.e the Tag is the index and the data is the rest Here's another variation: # Could be IO.read info = Hash[ *DATA.read.split( /\n\n+/ ).map{ |chunk| pieces = chunk.scan /^(\w+): (.+)/ first = pieces.shift raise "Uhm...I was assuming Tag comes first" unless first[0] == "Tag" [ first[1], Hash[ *pieces.flatten ] ] }.flatten ] require 'pp' pp info #=> {"ref4 "=>{"Author"=>"Jones, M B", "Type"=>"Book "}, #=> "ref3 "=>{"Author"=>"Williams, M ", "Type"=>"Conference Paper "}, #=> "ref2 "=>{"Author"=>"Smith, J ", "Type"=>"Journal "}, #=> "ref1 "=>{"Author"=>"Little, S R ", "Type"=>"Book "}} __END__ Tag: ref1 Type: Book Author: Little, S R Tag: ref2 Type: Journal Author: Smith, J Tag: ref3 Type: Conference Paper Author: Williams, M Tag: ref4 Type: Book Author: Jones, M B