File.open("#{__dir__}/input.txt",'r') do |file|
file.each_line do |line|
if /^\s+([A-Z][a-z]+)\s*$/ === line @line_number = file.lineno
break
end
end
end #puts fruits_line_number
##below I am tying to line number to a value. But #each_line method
starts ##reading lines from the beginning, instead of starting from the
line ##number I am trying to set.
File.open("#{__dir__}/input.txt",'r') do |file|
p file.lineno = @line_number
file.each_line do |line|
case line
# This will find the parent fruits/vegetables
when /^\s+([A-Z][a-z]+)\s*$/ @parent_fruit = line.strip
# This will match sub classes of the parent fruit/vegetable class
when /^([A-Z][a-z]+)\s?\/?(\s?[A-Z][a-z]+\s?\/?)*\s*$/ @fruit_and_kinds_array.clear unless @fruit_and_kinds_array.empty? @fruit_and_kinds_array << line.strip
when /^\s*$/m,/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
# blank line or line having email addresses, so do nothing
# This will match all the header names of the actual data
when /\b(^variety|kg|pp)\b/i @header_array.clear unless @header_array.empty? @header_array.concat line.strip.split
else
# this will collect all the actual fruits/vegetables data
puts @data_array << line.strip.unpack("A25A12A15A6A*") @csv_array += @fruit_and_kinds_array + @header_array + @data_array[0..-2] + @data_array[-1].split @data_array.clear
puts @csv_array
end
end
end
##below I am tying to line number to a value. But #each_line method
starts ##reading lines from the beginning, instead of starting from the
line ##number I am trying to set.
Yes, this is the intended behaviour.
And IO#lineno= doesn't "forward" to the linenumber.
It just set the line counter to a given value. It's like saying, now,
this line will be called "Line number X" (even if it's the Yth line of
the file).
elow I am tying to line number to a value. But #each_line method
starts ##reading lines from the beginning, instead of starting from the
line ##number I am trying to set.
with f.seek you can pre-position reading cursor
···
====================
position=-1
File.open(__FILE__,'r') do |file|
position=file.tell while (line=file.gets) && line !~ /#{'X'*3}/
break
end
#
File.open(__FILE__,'r') do |file| # XXX
file.seek(position)
file.each_line do |line|
puts "%4d | %-s" % [file.lineno,line]
end
end if position>=0
$ ruby essai.rb
1 | File.open(__FILE__,'r') do |file| # XXX
2 | file.seek(position)
3 | file.each_line do |line|
4 | puts "%4d | %-s" % [file.lineno,line]
5 | end
6 | end if position>=0
##below I am tying to line number to a value. But #each_line method
starts ##reading lines from the beginning, instead of starting from the
line ##number I am trying to set.
Yes, this is the intended behaviour.
And IO#lineno= doesn't "forward" to the linenumber.
It just set the line counter to a given value. It's like saying, now,
this line will be called "Line number X" (even if it's the Yth line of
the file).
I am having file, where line number varies between 99967-10,000 as far
as I have seen. The file header has lots of data, which I don't want to
extract. Thus I first search a word and make a note the line number of
that word. Now my next task is to directly start from that line number
till the end. As you can see, I am using lots of regex on each line. So
I don't want to apply all the regex to the lines of data, which I don't
need.
Can anyone give the idea, about this. as `File#lineno=` not helping me
on this. What are the other approaches I can apply there ?