From: Greg Willits Date: 2008-06-10T07:55:07+09:00 Subject: Re: Parsing a comma-separated file > name, age, gender > Bob, 32, M > Stacy, 14, F > ... > How do I parse 'Bob', knowing it's the first element on the line, '32' > is the second, 'M' is the last...I've been reading about regular > expressions. Is this the best way to solve this problem? And how exactly > do you use them? This doesn't handle all CSV specs, but if you know you have pure data like you show above, these are the rudimentary steps without the one-liner tricks, so it should be pretty straight forward to understand each step. Arranging them as methods to a class would be good. # read the file into a var if FileTest::exist?(file_name) file_lines = IO.readlines(file_name) end # normalize line endings so it doesn't matter what they are file_lines.strip! file_lines.gsub!(/\r\n/,'\n') file_lines.gsub!(/\r/,'\n') # normalize comma delimiters so it doesn't matter # if you have one, two or one,two or one , two etc... file_lines.gsub!(/\s*,\s*/, ',') # split lines into a single array of lines lines_array = file_lines.split('\n') # split each line into an array final_data = [] lines_array.each do |this_line| final_data << this_line.split(',') end # final_data is now an array of arrays that looks like this: [ ['name', 'age', 'gender'], ['Bob', '32', 'M'], ['Stacy', '14', 'F'] ] So, to get Bob, you'd have to know his line number, and index into the record array: final_data[1][0] # Bob final_data[2][3] # F -- greg willits -- Posted via http://www.ruby-forum.com/.