Splitting a string by x number of lines

Discussion in 'Ruby' started by lrlebron@gmail.com, Sep 20, 2007.

  1. Guest

    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
     
    , Sep 20, 2007
    #1
    1. Advertisements

  2. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 20, 2007
    #2
    1. Advertisements

  3. Guest

    On Sep 20, 1:02 pm, Sebastian Hungerecker <>
    wrote:
    > 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:
    > ICQ: 205544826


    Thanks that did the trick
     
    , Sep 20, 2007
    #3
  4. Jovino Guest

    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: [mailto:]
    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
     
    Jovino, Sep 20, 2007
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mothra
    Replies:
    1
    Views:
    491
    vkeyboard
    Jan 12, 2005
  2. Nakamura

    splitting a long token across lines

    Nakamura, Apr 4, 2004, in forum: Python
    Replies:
    4
    Views:
    438
  3. Peter Machell

    Splitting lines from a database query

    Peter Machell, Dec 26, 2006, in forum: Python
    Replies:
    6
    Views:
    621
    John Machin
    Dec 26, 2006
  4. Replies:
    1
    Views:
    602
    Jonathan Mcdougall
    Dec 6, 2005
  5. Brian

    splitting lines of code

    Brian, Jan 20, 2004, in forum: ASP General
    Replies:
    2
    Views:
    153
    Tim Slattery
    Jan 21, 2004
  6. Bob Smith

    splitting lines

    Bob Smith, Sep 22, 2003, in forum: Perl Misc
    Replies:
    7
    Views:
    275
    Mala Ananthamurthy
    Sep 23, 2003
  7. Jan Biel
    Replies:
    2
    Views:
    293
    GreenLight
    Apr 2, 2004
  8. Mothra
    Replies:
    3
    Views:
    174
    Anno Siegel
    Jan 12, 2005
Loading...