From: MonkeeSage Date: 2007-12-06T01:07:11+09:00 Subject: Re: hash On Dec 5, 9:26 am, Johnathan Smith wrote: > > Why do you repost the same question when you got answers already? Are > > those answers not propagated to ruby-forum? > > hi > > i wasnt sure i had recieved the answers i was looking for. I was > provided with a few different bits of code which im very grateful for > but I was unable to get working. one of the solutions provided by the > previous threat i will post below. Im not provided with any output at > all. I was to maintain the tag count as well as creating the hash. if > you could point out my errors id be extremly grateful > > thanks > > # > require 'getoptlong' > > opts = GetoptLong.new( > ['--style', '-n', GetoptLong::NO_ARGUMENT ], > ['--database', '-i', GetoptLong::REQUIRED_ARGUMENT] > ) > > $linecount = 0 > > opts.each do |opt, arg| > case opt > when '--style' > require arg > when '--database' > end > end > # > # > # > # process options > # > # > # > File.open('reference.txt').each do |line| > if line =~ /^tag:/i > $linecount += 1 > end > end > puts $linecount > > linecount = 0 > results = [] > hash = {} > File.open('reference.txt').each do |line| > m = line.match /^(\w+):\s*([\w+,\s]+)$/ > unless m > results << hash unless hash.empty? > hash = {} > else > linecount += 1 > hash[m[1]] = m[2].chomp > end > end > # > # > # > > -- > Posted viahttp://www.ruby-forum.com/. If you don't understand or don't find an answer sufficient, say so in the same thread, no one will be mad. But if you start a new one, then people following the old one aren't helped if you get the answer in the new one. Assuming that your text file is always in the format you listed, this may be what you're after... database = {} File.open('reference.txt') { | handle | tags = handle.read.split("\n\n") for tag in tags key, type, author = tag.split("\n") database[key[5..-1]] = [type[6..-1], author[9..-1]] end } p database Given your sample data, this will output the following... {"ref1"=>["Book", "Little, S R"], "ref2"=>["Journal", "Smith, J"], "ref3"=>["Conference Paper", "Williams, M"]} And the number of tags seen is just "database.length". Regards, Jordan