-3

I'm trying to read from a CSV file and codify people into groups using an equation. I append the name of their group they fall into to the end of the array that their row creates. Then I write it to a new file so I don't overwrite the original file in case something goes wrong.

Also, it loops through the directory and does this for multiple csv files.

However, when I try to open the new file to write into, it's saying this:

Segmenter.rb:12:in `open': wrong number of arguments (0 for 1) (ArgumentError)
        from Segmenter.rb:12:in `foreach'
        from Segmenter.rb:12:in `<main>'

Here is the script with the coefficients removed. All the x's in the coefficients array are numbers in my script.

require 'csv'

coefficients = [ 
[x, x, x, x, x, x, x, x, x, "Utilitarians"],
[x, x, x, x, x, x, x, x, x, "Hometown School/Social"],
[x, x, x, x, x, x, x, x, x, "State Pride"],
[x, x, x, x, x, x, x, x, x, "Hard-Wired Advocates"],
[x, x, x, x, x, x, x, x, x, "Game Hunters"]
]

Dir.foreach do |current_file|

    data_set = CSV.read(current_file)

    data_set.array.each do |row|
        segment_value = 0
        segment_name = ""
        coefficients.each do |segment|
            if (segment[0] *  row[1] + segment[1]*row[2] + segment[2]*row[3] + segment[3]*row[4] + segment[4]*row[5] + segment[5]*row[6] + segment[6]*row[7] + segment[7]*row[8]) > segment_value
                segment_value = segment[0] *  row[1] + segment[1]*row[2] + segment[2]*row[3] + segment[3]*row[4] + segment[4]*row[5] + segment[5]*row[6] + segment[6]*row[7] + segment[7]*row[8]
                segment_name = segment[8]
            end
        row << segment_name
    end

    CSV.open("#{current_file.basename} SEGMENTED.csv", "w") do |writer|
        data_set.array.each do |data|
            writer << data
        end
    end

    end 

end
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
ABvsPred
  • 67
  • 7
  • stupid question, but have you saved your code after changes? It says that the error is on line 12, but your open method is not even near it – AKovtunov Jun 15 '15 at 00:24
  • Yeah. Its somehow rolling it in with the Dir.foreach call. Anyway, that foreach call is on line 11 I think. I took out an unnecessary require after copying in the error. Then I copied in the actual script. Miniscule change that shouldnt matter. – ABvsPred Jun 15 '15 at 00:30
  • Also no harm in asking. I called cat Segmenter.rb to make sure that wasnt the problem. Haha. – ABvsPred Jun 15 '15 at 00:31
  • What is your question? – sawa Jun 15 '15 at 01:18
  • It wont let me open a csv file to write into. I don't understand why, and its also saying there are 0 arguments, and I supplied the filename and open type, so you would think there are two arguments. – ABvsPred Jun 15 '15 at 01:24
  • 1
    If you’ve solved your problem, you should either post your answer below, which you can accept after 48 hours, or, if you think your question/answer are of no relevance to future visitors, you can delete your question entirely. You should *not* simply edit your question with the solution. – Andrew Marshall Jun 15 '15 at 06:23

1 Answers1

1

I believe the problem is with Dir.foreach, not CSV.open.

You need to supply a directory to foreach as an argument. That's why you are getting the missing argument error.

Try:

Dir.foreach('/path/to_my/directory') do |current_file|

I think the open that is referenced in the error message is when Dir is trying to find the directory to open in order to get the file list for foreach.

Joseph
  • 760
  • 1
  • 4
  • 9