Getting the next line with the iterator

Hi,

I would like to know if there is a ruby way to achieve what I want to
do. I want to be able to access the next iterator while reading a file.
Basically, I want to be able to collect the next 3 lines when I
encounter a certain line:

list = []

File.open(‘toto.txt’, ‘rb’) do |myFile|
myFile.each {|line|

	    if line =~ /regExp/
	        list << line.nextIterator
	        list << line.nextIterator
	        list << line.nextIterator
	    end
}

end
puts list

This code is not working, I know. The method “nextIterator” doesn’t
exist. This is the way I see it, but I don’t know how to do it. I think
that you can invode the method “next” on iterators in Python. I don’t
know how to do that without invoking the command ‘next’ with a flag and
a counter, a little bit like this code (which is not very rubyist):

list = []
flagIn = false
counter = 3

File.open(‘toto.txt’, ‘rb’) do |myFile|
myFile.each {|line|

    if line =~ /regExp/i
        flagIn = true
        next
    end

    if flagIn
        list << line
        counter -= 1
        flagIn = false if counter == 0
    end
}

end
puts list

Any suggestions?

Thanks

On Apr 27, 2006, at 6:20 PM, Eric B. wrote:

File.open(‘toto.txt’, ‘rb’) do |myFile|

flagIn = false
if flagIn

Thanks

Posted with http://DevLists.com. Sign up and save your mailbox.

This is totally untested but perhaps:
list = []
File.open(…) do |file|
file.each do |line|
if line =~ /regexp/
3.times { list << file.gets }
end
end

How about reading the enire file in an array, something like (untested
code):

arr = IO.readlines(‘toto.txt’)
list = []
arr.each_with_index{|line,i|
if line =~ /regExp/
list << line[i]
list << line[i+1]
list << line[i+2]
end
}

This may not work as is, but will give you an idea …
HTH,

– shanko

Eric B. [email protected] wrote:
Hi,

I would like to know if there is a ruby way to achieve what I want to
do. I want to be able to access the next iterator while reading a file.
Basically, I want to be able to collect the next 3 lines when I
encounter a certain line:

list = []

File.open(‘toto.txt’, ‘rb’) do |myFile|
myFile.each {|line|

if line =~ /regExp/
list << line.nextIterator
list << line.nextIterator
list << line.nextIterator
end
}
end
puts list

This code is not working, I know. The method “nextIterator” doesn’t
exist. This is the way I see it, but I don’t know how to do it. I think
that you can invode the method “next” on iterators in Python. I don’t
know how to do that without invoking the command ‘next’ with a flag and
a counter, a little bit like this code (which is not very rubyist):

list = []
flagIn = false
counter = 3

File.open(‘toto.txt’, ‘rb’) do |myFile|
myFile.each {|line|

if line =~ /regExp/i
flagIn = true
next
end

if flagIn
list << line
counter -= 1
flagIn = false if counter == 0
end
}
end
puts list

Any suggestions?

Thanks

Posted with http://DevLists.com. Sign up and save your mailbox.

Oops, made a mistake :slight_smile:

Shashank D. [email protected] wrote: list << line[i]
list << line[i+1]
list << line[i+2]
^^^^^^^^^^^

This should have read:

list << arr[i]
list << arr[i+1]
list << arr[i+2]

Sorry for the confusion …

– shanko

On Apr 27, 2006, at 11:20 pm, Eric B. wrote:

I would like to know if there is a ruby way to achieve what I want to
do. I want to be able to access the next iterator while reading a
file.
Basically, I want to be able to collect the next 3 lines when I
encounter a certain line

Eric

How bout this?

list = []
File.open(‘testdata.txt’) do |file|
start_line = -1
file.each do |line|
start_line = $. if line =~ /start_here/
list << line if ($. == start_line + 1) … ($. == start_line + 3)
end
end

Ashley

On Apr 27, 2006, at 11:56 pm, Ashley M. wrote:

start_line = $. if line =~ /start_here/
list << line if ($. == start_line + 1) .. ($. == start_line + 3)

Oh I forgot to say… anyone know how to avoid the horrible Perlism
($.)?

On 4/27/06, Eric B. [email protected] wrote:

Hi,

I would like to know if there is a ruby way to achieve what I want to
do. I want to be able to access the next iterator while reading a file.
Basically, I want to be able to collect the next 3 lines when I
encounter a certain line:

Something like this maybe?

list = []
matched = -1
File.open(‘ruby.txt’, ‘rb’) do |myFile|
myFile.each {|line|
matched = 0 if line =~ /regExp/
list << line if matched > 0
matched = matched > 2 ? -1 : matched + 1
}
end
puts list

Thanks a lot everybody for all your good answers. Logan, this is exactly
what I wanted. Ashley, if you want to avoid the $. , maybe you can use
this:

require ‘English’

start_line = $INPUT_LINE_NUMBER if line =~ /start_here/

Thanks again to everybody!