text = <<-STR
Ram earns $2500 per month
Arup earns $5000 per month
Ayan earns $42,000 per mnth
Anup earns $225000 per month
Deba earns $450000 per month
STR
File.write(“test.csv”,text)
File.foreach(‘test.csv’,:skip_lines =>
/^[^0-9]+(\d{5,6})[^0-9]+$/,:col_sep => " ") do |csv|
p csv
end
>> “Ram earns $2500 per month\n”
>> “Arup earns $5000 per month\n”
>> “Ayan earns $42,000 per mnth\n”
>> “Anup earns $225000 per month\n” # should be skipped
>> “Deba earns $450000 per month\n” # should be skipped
I don’t think File’s (or IO’s) foreach understands :skip_lines, does
it? I think you mean: CSV.foreach
You are right. I don’t know, why I couldn’t see that mistake by mean. It
is now working.
require ‘csv’
text = <<-STR
Ram earns $2500 per month
Arup earns $5000 per month
Ayan earns $42,000 per mnth
Anup earns $225000 per month
Deba earns $450000 per month
STR
File.write(“test.csv”,text)
CSV.foreach(‘test.csv’,:skip_lines =>
/^[^0-9]+(\d{5,6})[^0-9]+$/,:col_sep => " ") do |csv|
p csv
end
>> [“Ram”, “earns”, “$2500”, “per”, “month”]
>> [“Arup”, “earns”, “$5000”, “per”, “month”]
>> [“Ayan”, “earns”, “$42,000”, “per”, “mnth”]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.