Splitting a string by x number of lines

L

lrlebron

I am trying to figure out how to split a string into chunks of 25
lines or less to pass to another function

Any ideas on how I could approach this?

thanks,

Luis
 
S

Sebastian Hungerecker

I am trying to figure out how to split a string into chunks of 25
lines or less to pass to another function

Any ideas on how I could approach this?

string.scan(/(?:.*?\n){1,25}/)


HTH,
Sebastian
 
J

Jovino

I think you can get with this:

def split_newlines(str, num_lines)
result,prev,index = 0,0,0
array = []

until result.nil?
result = str.index("\n", result)
unless result.nil?
index += 1
array << str[prev..result] if index%num_lines==0
result += 1
end
prev = result if index%num_lines==0 and result
end
array << str[prev..str.size] unless prev == str.size
end

But it seems too complicated for me.
I'm sure there is a very better approach but I'm not an experienced ruby
programmer. I like very much to see better responses for learn something.

Regards,

Jovino

-----Mensaje original-----
De: (e-mail address removed) [mailto:[email protected]]
Enviado el: jueves, 20 de septiembre de 2007 19:40
Para: ruby-talk ML
Asunto: Splitting a string by x number of lines

I am trying to figure out how to split a string into chunks of 25
lines or less to pass to another function

Any ideas on how I could approach this?

thanks,

Luis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top