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

···

--
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

···

On Apr 27, 2006, at 6:20 PM, Eric Boucher 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
          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.

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 Boucher <devlists-ruby-talk@devlists.com> 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.

---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.

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:20 pm, Eric Boucher 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

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

···

On 4/27/06, Eric Boucher <devlists-ruby-talk@devlists.com> 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:

--
Bill Guindon (aka aGorilla)
The best answer to most questions is "it depends".

Oops, made a mistake :slight_smile:

Shashank Date <shanko_date@yahoo.com> 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

···

---------------------------------
Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates.

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

···

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

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

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!

···

On Friday, April 28, 2006, at 8:00 AM, Ashley Moran wrote:

On Apr 27, 2006, at 11:56 pm, Ashley Moran 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 ($.)?

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