Phrogz
12/4/2007 3:20:00 PM
On Dec 4, 7:30 am, Johnathan Smith <stu...@hotmail.com> 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
>
> if anyone could provide me with any psuedo code i'd be very appreciative
> my code is below
>
> Many Thanks
>
> text file:
>
> 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
info = {}
last_tag = nil
# Could be File.readlines
DATA.each_line{ |line|
_, key, data = line.match( /^(\w+): (.+)/ ).to_a
next unless key # skip blank lines
if key == "Tag"
last_tag = data
info[ data ] = {}
else
info[ last_tag ][ key ] = data
end
}
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