Reputation: 185
I'm trying to write an CSV parser. Each line has multiple fields in which I need to process. Each line represents patient data, so I need each line processed by itself. Once I'm finished processing each line I need to go to the next until the end of the file is reached.
I've successfully started writing the parser in Ruby. The data is getting imported and it's creating an array of arrays (each line is an array).
The problem I'm having is properly looping through the data line by line. So, right now I can successfully process the first line and parse each field. I start running into a problem when I add another line with new patient data. The second line gets processed and added to the new array that has been created. For example, line 1 and line 2 once processed, get added to one big array instead of an array of arrays. The data imported needs to output in the same structure.
Here is my code so far:
original_data = Array.new
converted_data = Array.new
Dir.chdir 'convert'
CSV.foreach('CAREPRODEMO.CSV') do |raw_file|
original_data << raw_file
end
# Needed at beginning of array for each patient
converted_data.insert(0, 'Acvite', 'ACT')
# Start processing fields
original_data.each do |o|
# BEGIN Check for nil in original data and replace with empty string
o.map! { |x| x ? x : ''}
converted_data << o.slice(0)
# Remove leading zeros from account number
converted_data[2].slice!(0)
if converted_data[2].slice(1) == '0'
converted_data[2].slice!(1)
end
# Setup patient name to be processed
patient_name = Array.new
patient_name << o.slice(3..4)
converted_data << patient_name.join(' ')
# Setup patient address to be processed
patient_address = Array.new
patient_address << o.slice(5)
converted_data << patient_address.join(' ')
# END Check for nil in converted data and replace with empty string
converted_data.map! { |x| x ? x : ''}
end
# For debugging
p converted_data
Output:
["Acvite", "ACT", "D65188596", "SILLS DALTON H", "16243 B L RD", "00D015188596", "BALLARD DAVE H", "243 H L RD", "", "", ""]
Wanted:
["Acvite", "ACT", "D65188596", "SILLS DALTON H", "16243 B L RD"]
["Acvite", "ACT", "D15188596", "BALLARD DAVE H", "243 H L RD"]
Upvotes: 0
Views: 306
Reputation: 18772
You need to use array of array for storing results, you are using single array, hence the output that you have mentioned.
Move converted_data array inside the loop, and define a new array for collecting output of each loop. A possible approach is shown below.
original_data = Array.new
# Changed the variable name from converted_data
final_data = Array.new
...
original_data.each do |o|
converted_data = Array.new
...
# END Check for nil in converted data and replace with empty string
converted_data.map! { |x| x ? x : ''}
final_data << converted_data
end
p final_data
Upvotes: 1