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
I believe the problem is with
Dir.foreach
, notCSV.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 whenDir
is trying to find the directory to open in order to get the file list forforeach
.