Parsing a line into a hash table

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.)

Thanks.

-Kurt Euler

In article C47CCC6238EFD4119C5200508B95A100074AFB53@cup1ex1.portal.com,
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