-1

I am building a script that takes in a column from a CSV that can contain 0 or more ID numbers. I have created an array of the column, however, since some cells have no ID number and some have multiple,I have an array of arrays.

I want to create an array where each element is a single ID (i.e split the IDs from each element in the array to a single element).

Here is my code so far:

require 'csv'
class MetadataTherapyParser

    def initialize (csv)
        @csv = csv
    end

    def parse_csv
        therapy_array = []
        CSV.foreach(@csv) do |csv_row|
            therapy_array << csv_row[0] 
        end
        therapy_array
    end

    def parse_therapies(therapy_array) 
        parsed_therapy_array = therapy_array.flatten! 
    end
end
metadata_parse = MetadataTherapyParser.new ("my_path.csv")
therapy_array = metadata_parse.parse_csv
metadata_parse.parse_therapies(therapy_array)
p therapy_array

However, the output is still an array of arrays. I am thinking it may have something to do with nil values? I have tried looking for answers online to no avail.

If you could give me some advice as how to fix this problem, it would be greatly appreciated!

Thank you in advance.

EDIT I have posted a snippet of my output below. It still appears to be a nested array.

[nil, nil, "57e923a0f5c3c85c9200052b, 58b828f4f5c3c806490046a6", "57e923a0f5c3c85c9200052b, 4ffaf15af758862fb10155e3, 58b828f4f5c3c806490046a6", "57e923a0f5c3c85c9200052b, 4ffaf15af758862fb10155e3, 58b828f4f5c3c806490046a6", nil, nil, nil, nil, nil, "5f9176e50cf19216d6da9289", "6082f6bd0cf19225863fc985", "6082f6fd0cf192258d3fce0e", "6082f69e0cf19225ac3fc551", "6082f6a60cf19225a23fd3e4, 6082f6d30cf192258d3fce0a, 6082f7fa0cf19225953fc77c"]

Belle
  • 11
  • 5
  • `therapy_array.flatten!` is the version that applies to the originating object ... do you need the exclamation mark? – Jad Apr 27 '21 at 14:09
  • @jad I don't think so but I tried to remove it and still the same issue – Belle Apr 27 '21 at 14:11
  • 2
    Please post some actual input and an example of expected output. – Todd A. Jacobs Apr 27 '21 at 14:21
  • As written, you need the `!` version because you don't assign the return value from `parse_therapies` to a variable. You didn't supply any test data, but when I hardwired `parse_csv` to return `[[0,1,2,[3,4,5]],%w(a b c)]` the results were properly flattened by `parse_therapies` so perhaps there's a problem with your input data. – pjs Apr 27 '21 at 14:34
  • I will post an sample of my output – Belle Apr 27 '21 at 14:54
  • Your question is not clear. Readers need to know what your CSV file looks like and how you wish to manipulate that information. The easiest way of doing that is to give an example CSV file and show the Ruby object that is the desired result for that file (as @Todd requested). The example should be no larger than is necessary to show what you are trying to do. – Cary Swoveland Apr 27 '21 at 17:30
  • @Belle that `Array` looks flat to me (no Arrays nested inside). What were you expecting? – engineersmnky Apr 28 '21 at 15:45

2 Answers2

1

You say you have "an array of arrays" but your example array is "57e923a0f5c3c85c9200052b, 4ffaf15af758862fb10155e3, 58b828f4f5c3c806490046a6" ... that's not an array. That's a string. You probably want to split strings that have commas into separate array elements.

So instead of

therapy_array << csv_row[0] 

try instead

therapy_array << csv_row[0].to_s.split(',').map(&:strip)
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
0

the flatten is working perfectly. the issue you are having is that you have lots of strings in your output, with commas in them.

having not got a copy of your CSV, I'm going to assume that it has been parsed correctly, and that you do want to keep the contents of the first cell as it is:

def parse_therapies(therapy_array) 
  parsed_therapy_array = therapy_array.map { |x| x && x.split(/,/) }.flatten.compact
  therapy_array.replace(parsed_therapy_array)
end

this will also remove all the nil elements, assuming you don't want them, using the compact procedure.

Jad
  • 1,257
  • 12
  • 19