From: HarryO Date: 2001-12-30T13:37:59+09:00 Subject: [ruby-talk:29702] Re: comma-delimited to an array On Sun, 30 Dec 2001 13:06:26 +1100, nobu.nokada wrote: >> num,date,time,orig,type,action,rule,iface >> 1,28Dec2001,23:59:51,121.210.49.22,log,accept,16,eth-s1p2c0 >> 2,28Dec2001,23:59:44,121.210.48.11,log,drop,20,eth-s1p1c0 >> 3,28Dec2001,23:59:57,121.210.49.22,log,accept,16,eth-s1p1c0 >> > Possibly, you may want to access each elements by names in the first > line? > > data = [] > open(file) do |f| > foo = Struct.new(nil, *f.gets.chomp.split(",")) f.each do |line| > bar = foo.new(*line.chomp.split(",")) data[bar.num.to_i] = bar > end > end What an excellent example of using Struct! I would have used arrays of hashes and the code would have been a lot messier. This will definitely go into my personal list of Ruby idioms.