beginner question - searching csv file by column and returningvalues if true

B

Bob West

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
 
W

w_a_x_man

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

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
 
J

Jeremy Bopp

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.

require 'csv'

CSV.open('your-file.csv', 'r') do |row|
puts(row[5] == 'true' ? row[6] : row[7])
end


This uses the csv module to parse the file and iterate row by row
through the data. It checks if the 6th column is 'true' and prints the
7th column if so or the 8th column otherwise. This code should be made
more robust by at least checking for invalid data in the 6th column
rather than assuming that any value other than 'true' is the same as
'false'.

-Jeremy
 
C

Charles Calvert

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).
[snip]

require 'csv'

Note: if you're using 1.8, I'd suggest using the FasterCSV gem instead
of the standard CSV class. If you're using 1.9, FasterCSV is already
built in as CSV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top