On Monday 15 October 2007 12:59 pm, 7stud -- wrote:
> Have you seen each_slice() before?

Thanks!  I've read (or skimmed, or attempted to read) enough Ruby books that 
I'm fairly certain I must have seen it--recalling it is another thing. ;-)

BTW, I don't think it's applicable in my particular case--the situation is a 
little more complex than I led you to believe.  At some point I may be 
posting my (almost) finished code for a round of constructive criticism, and 
then you might be able to see what I mean.

In a brief attempt to give a hint:

2 (specific) lines in the input file become 3 in the output
3 (specific) lines in the input file become 3 in the output
4 (specific) lines in the input file can become 3 or 4 in the output
5 (specific) lines in the input file can become 3, 4, or 5 in the output
...
and other lines (not those specific lines) are simply copied from the input to 
the output

Furthermore, those specific lines are of two different (general) 
types--"general" intended to hint that the 2nd specific type of line comes in 
two different subtypes.  In addition, note that those specific lines contain 
different text, i.e., they are titles for the records I'm processing.

Randy Kramer

> 
> require 'enumerator'
> 
> #create a file with some data:
> File.open("data.txt", "w") do |file|
>   (1..22).each do |i|
>     file.puts("line #{i}")
>   end
> end
> 
> #read the file in groups of 5 lines:
> File.open("data.txt") do |file|
>   file.each_slice(5) do |lines|
>     p lines
>   end
> end
> 
> 
> --output:--
> ["line 1\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n"]
> ["line 6\n", "line 7\n", "line 8\n", "line 9\n", "line 10\n"]
> ["line 11\n", "line 12\n", "line 13\n", "line 14\n", "line 15\n"]
> ["line 16\n", "line 17\n", "line 18\n", "line 19\n", "line 20\n"]
> ["line 21\n", "line 22\n"]
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 
>