how to wrap a long string

Discussion in 'Ruby' started by Li Chen, Sep 25, 2008.

  1. Li Chen

    Li Chen Guest

    Hi all,

    I need to wrap a long string into several small segments.
    I search the forum and see a few solutions for that. I copy one script
    and run it on my Vista(with very small change) and here are the output:


    C:\Documents and Settings\chen73>irb
    irb(main):001:0> text = 'aaaaa11111AAAAA22222BBBBB'
    => "aaaaa11111AAAAA22222BBBBB"
    irb(main):002:0> 0.step(text.length, 5) {|x| puts text[x, x+5]}
    aaaaa
    11111AAAAA
    AAAAA22222BBBBB
    22222BBBBB
    BBBBB

    => 0

    It looks like the script really wraps the string. But it also repeats
    printing out some stuff, such two times of 'AAAAA', '22222', 3 times of
    'BBBBB'.
    Is it possible to get the output as follows:

    aaaaa
    11111
    AAAAA
    22222
    BBBBB

    Thanks,

    Li
    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Sep 25, 2008
    #1
    1. Advertising

  2. Li Chen

    Thomas B. Guest

    Li Chen wrote:
    > text[x, x+5]


    You wanted either text[x..(x+5)] or better text[x,5]. Have a look at
    String#[].

    TPR.
    --
    Posted via http://www.ruby-forum.com/.
    Thomas B., Sep 25, 2008
    #2
    1. Advertising

  3. Li Chen

    Lex Williams Guest

    Considering that :

    a = "1112223334444"

    watch the following irb session :

    irb(main):010:0> a.scan(/((.)\2+)/).collect {|match| match[0]}
    => ["111", "222", "333", "4444"]

    using that regex , you'll obtain an array of matches .
    --
    Posted via http://www.ruby-forum.com/.
    Lex Williams, Sep 25, 2008
    #3
  4. Karl-Heinz Wild, Sep 25, 2008
    #4
  5. On 25.09.2008, at 16:43, Lex Williams wrote:

    > Considering that :
    >
    > a = "1112223334444"
    >
    > watch the following irb session :
    >
    > irb(main):010:0> a.scan(/((.)\2+)/).collect {|match| match[0]}
    > => ["111", "222", "333", "4444"]


    great idea!
    ... this will also match single letters / digits

    irb(main):020:0> a = "11111122233344444445556667778888888AAABCCDEEEEF"
    => "11111122233344444445556667778888888AAABCCDEEEEF"
    irb(main):021:0> puts a.scan( /((.)\2+|.)/ ).collect { |x| x[0] }
    111111
    222
    333
    4444444
    555
    666
    777
    8888888
    AAA
    B
    CC
    D
    EEEE
    F

    regards
    Karl-Heinz
    Karl-Heinz Wild, Sep 25, 2008
    #5
  6. You'll miss the unrepeated characters:

    > ruby -e 'p "abbcccdddd".scan(/((.)\2+)/).transpose.shift'

    ["bb", "ccc", "dddd"]

    Where did "a" go?

    Change the "+" into "*" and it'll work:

    > ruby -e 'p "abbcccdddd".scan(/((.)\2*)/).transpose.shift'

    ["a", "bb", "ccc", "dddd"]

    gegroet,
    Erik V. - http://www.erikveen.dds.nl/
    Erik Veenstra, Sep 25, 2008
    #6
    1. Advertising

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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    672
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,184
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    693
    Chronic Philharmonic
    May 10, 2008
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    226
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    242
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page