From: Mike Stok Date: 2002-09-11T02:54:54+09:00 Subject: Re: Parsing a line into a hash table... In article , Kurt Euler wrote: > All- > > Currently I parse tab-delimited lines in a text file into a 1D array > called "field" thusly: > > IO.foreach("./control/packages.txt") { |x| > field = x.chop.split('\t') > do_stuff > } > > Question: How can I parse these lines into values for a pre-defined hash > table. (My objective is to give each field a name rather than a number.) One way to do it might be a struct e.g. Fields = Struct.new("Fields", :first, :second, :third) # ... IO.foreach("./control/packages.txt") { |line| fields = Fields.new(*line.chop.split(/\t/)) # use fields['first'] or fields.first ... } You might want to do some error checking to see if you get the right number of fields from your input, in my example Fields.members.size is 3. Hope this helps, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA