From: Michael Neumann Date: 2001-12-30T19:40:47+09:00 Subject: [ruby-talk:29715] Re: comma-delimited to an array Dash Riprock wrote: > > I'm quite new to Ruby, so please be gentle. > > I've got a file in which each line has comma-delimited items. A > sample might look like this: > > 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 > > Because the order of the items may *vary* from day to day, I want to > treat each line as an array. > > But the question is, as I read each line, how do I convert that > single line into an array? > > I've tried quite a number of things and I can't figure it out. Either use the CSV library (you'll find that at Ruby's Application Archive), or simply by spliting the line: line = "1,28Dec2001,....." arr = line.split(",") Or if you want to remove whitespaces from each column, try this: arr = line.split /\s*,\s*/ Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de