[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Splitting a string by x number of lines

lrlebron@gmail.com

9/20/2007 5:37:00 PM

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

3 Answers

Sebastian Hungerecker

9/20/2007 6:03:00 PM

0

lrlebron@gmail.com wrote:
> 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
--
NP: Die Apokalyptischen Reiter - Himmelskind
Jabber: sepp2k@jabber.org
ICQ: 205544826

lrlebron@gmail.com

9/20/2007 6:28:00 PM

0

On Sep 20, 1:02 pm, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> lrleb...@gmail.com wrote:
> > 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
> --
> NP: Die Apokalyptischen Reiter - Himmelskind
> Jabber: sep...@jabber.org
> ICQ: 205544826

Thanks that did the trick


Jovino

9/20/2007 7:39:00 PM

0

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: lrlebron@gmail.com [mailto:lrlebron@gmail.com]
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