From: w_a_x_man Date: 2010-11-05T03:35:12+09:00 Subject: Re: beginner question - searching csv file by column and returning values if true On Nov 4, 11:20 am, Bob West wrote: > I'm a complete noob to ruby and am hoping someone can help me with this. > > I have a csv file with one field called geo_enabled (the 6th column). > The values are true or false. If the value is true, I'd like to return > the values contained in the field called Geo (7th column). If it is > false, I'd like to return the values in the "Location" field (8th > column). > > I've got a rough idea how this should go. But it isn't close to being > working code. > > I'd really appreciate all help anyone can provide. > > -bob > > -- > Posted viahttp://www.ruby-forum.com/. Assuming that your data doesn't contain commas: # Read the file. rows = IO.readlines( "data1" ) # Remove linefeeds at end of lines. rows.map!{|line| line.chomp} values = [] rows.each_with_index{|row,i| # Skip first row (presumably headers). next if 0 == i columns = row.split( "," ) # Skip if less than 8 columns. next if columns.size < 8 # First column is columns[0], not columns[1]. if "true" == columns[5] values << columns[6] else values << columns[7] end } p values puts values