Iterate to next element in Ruby
Summary: Is there a way to get the next element in a loop without starting
from the beginning of the loop? I know that next allows for iterating to
the next element, but it starts at the beginning of the loop. I want to
get the next element and continue to the next statement in the loop.
Details:
I am parsing a text file that is structured as follows:
element 1
part 1
part 2
element 2
part 1
part 2
(note there are 11 parts per element this is just an example).
I read all of the text into an array and then I want to process each line
using something like:
text.each do |L|
#if L is new element create object to store data
#parse next line store as part 1
#parse next line store as part 2
end
I know how to do this with a while loop and a C-like coding style where I
explicitly index into the array and increment the index variable by the
amount I want each time. But I'm wondering if there is a Ruby way to do
this. Something analogous to next but that would continue from the same
point in the loop, just with the next element, rather than starting from
the beginning of the loop.
No comments:
Post a Comment