Forum: Ruby getting the next line with the iterator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric B. (Guest)
on 2006-04-28 02:21
(Received via mailing list)
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
Logan C. (Guest)
on 2006-04-28 02:30
(Received via mailing list)
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
Shashank D. (Guest)
on 2006-04-28 02:36
(Received via mailing list)
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. <removed_email_address@domain.invalid> 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.
Shashank D. (Guest)
on 2006-04-28 02:49
(Received via mailing list)
Oops, made a mistake :-)

Shashank D. <removed_email_address@domain.invalid> 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
Ashley M. (Guest)
on 2006-04-28 02:58
(Received via mailing list)
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
Ashley M. (Guest)
on 2006-04-28 03:01
(Received via mailing list)
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
($.)?
Bill G. (Guest)
on 2006-04-28 03:04
(Received via mailing list)
On 4/27/06, Eric B. <removed_email_address@domain.invalid> 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
Eric B. (Guest)
on 2006-04-28 05:45
(Received via mailing 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!
This topic is locked and can not be replied to.