How can skip the lines while using CSV by the option (:skip_lines)

Code I tried :

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)

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

Here is the rubular link : http://rubular.com/r/P99VIRvBqS

Where did I do wrong ?

On Sat, Feb 15, 2014 at 1:56 PM, Arup R. [email protected]
wrote:

STR

File.write(“test.csv”,text)

File.foreach(‘test.csv’,:skip_lines =>
/^[^0-9]+(\d{5,6})[^0-9]+$/,:col_sep => " ") do |csv|

I don’t think File’s (or IO’s) foreach understands :skip_lines, does
it? I think you mean: CSV.foreach

tamouse m. wrote in post #1136800:

On Sat, Feb 15, 2014 at 1:56 PM, Arup R. [email protected]
wrote:

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs