• recompose strings of limited length

    From Michael Uplawski@21:1/5 to All on Mon Dec 19 17:19:53 2016
    Good afternoon, all.

    I *have* a a working function, but I do not like it.

    What I *want to* do (nobody asks me): take a string of arbitrary
    length, or an array of such random strings and modify them in a way
    that, in the result, all strings are split up into chunks of a maximum string-length.

    This sounds and reads so simple that my current solutions make me feel
    as dumb as a brick. Here is one of my functions which transforms 1
    string:

    ------------
    def string_to_array(str, width)
    arr = str.split(/\s/)
    items = ''
    wwidth = 0
    arr.each do |w|
    wwidth += w.size + 1
    if( width < wwidth )
    items << "\n"
    wwidth = w.size + 1
    end
    items << w << ' '
    end
    # split up again or not, as needed
    items.split(/\n/)
    end
    -------------

    An exemplary call of this fuction:

    ---------------
    puts string_to_array(ARGV[0], ARGV[1])
    ---------------

    And the output on the command line:

    -----------
    me@here:/tmp$ ./test.rb "Ein König mit Krone ist besser als ohne" 15
    Ein König mit
    Krone ist
    besser als
    ohne
    -----------

    Yeah... I am so proud. Trying to simplify the working function, above, I
    use to mess it all up and after hours arrive at a new solution which
    tends to look terribly like the original. If you say that it must be
    ugly, I leave it this way.

    Thank you for any hints for an alternative implementation.

    Michael

    --
    GnuPG brainpoolP512r1/5C2A258D 2015-10-02 [expires: 2017-10-01]
    sub brainpoolP512r1/53461AFA 2015-10-02 [expires: 2017-10-01]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Michael Uplawski on Tue Dec 20 19:16:52 2016
    On 19.12.2016 17:19, Michael Uplawski wrote:

    What I *want to* do (nobody asks me): take a string of arbitrary
    length, or an array of such random strings and modify them in a way
    that, in the result, all strings are split up into chunks of a maximum string-length.

    max_len = 10
    strings = str.scan /.{1,#{max_len}}/

    -----------
    me@here:/tmp$ ./test.rb "Ein König mit Krone ist besser als ohne" 15
    Ein König mit
    Krone ist
    besser als
    ohne
    -----------

    You never mentioned that you want word splitting.

    irb(main):011:0> str = "Ein König mit Krone ist besser als ohne"
    "Ein König mit Krone ist besser als ohne"
    irb(main):015:0> str.scan %r{\S.{0,#{max_len - 1}}(?!\S)}
    ["Ein König mit", "Krone ist", "besser als ohne"]

    It seems your last two strings should be one.

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Uplawski@21:1/5 to Robert Klemme on Wed Dec 21 10:55:49 2016
    Good morning.

    On Tue, 20 Dec 2016 19:16:52 +0100,
    Robert Klemme <shortcutter@googlemail.com> wrote:
    On 19.12.2016 17:19, Michael Uplawski wrote:
    max_len = 10
    strings = str.scan /.{1,#{max_len}}/

    Thank you. This is clearly the function that I need.

    I am not jubilating for the simple reason, that I understand the
    documentation on String.scan only with this concrete example and already
    know, that I cannot use the function for anything that I have not yet
    seen in code.

    It solves my current problem, though.
    It seems your last two strings should be one.

    Right. That is a bug in my code.

    Nice holidays all, happy new year, too, within the limits of the
    televised joy.


    Cheers

    robert



    --
    GnuPG brainpoolP512r1/5C2A258D 2015-10-02 [expires: 2017-10-01]
    sub brainpoolP512r1/53461AFA 2015-10-02 [expires: 2017-10-01]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Michael Uplawski on Wed Dec 21 19:02:29 2016
    On 21.12.2016 10:55, Michael Uplawski wrote:
    Good morning.

    On Tue, 20 Dec 2016 19:16:52 +0100,
    Robert Klemme <shortcutter@googlemail.com> wrote:
    On 19.12.2016 17:19, Michael Uplawski wrote:
    max_len = 10
    strings = str.scan /.{1,#{max_len}}/

    Thank you. This is clearly the function that I need.

    Good!

    I am not jubilating for the simple reason, that I understand the documentation on String.scan only with this concrete example and already know, that I cannot use the function for anything that I have not yet
    seen in code.

    You should really get familiar with regular expressions. One book I
    usually recommend is "Mastering Regular Expressions" (O'Reilly).

    There is also a nice programm called "Regex Coach" which allows to watch
    a regex engine match. This is really helpful for learning. Runs under
    WINE as well.

    http://weitz.de/regex-coach/

    It solves my current problem, though.

    But note the form above does _not_ do word splitting. For that you need
    the other one.

    Nice holidays all, happy new year, too, within the limits of the
    televised joy.

    Same to you!

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Uplawski@21:1/5 to Robert Klemme on Thu Dec 22 15:34:00 2016
    On Wed, 21 Dec 2016 19:02:29 +0100,
    Robert Klemme <shortcutter@googlemail.com> wrote:

    You should really get familiar with regular expressions. One book I
    usually recommend is "Mastering Regular Expressions" (O'Reilly).

    You are probably right. The difficulties that I have with String.scan
    (and functions documented in a similar way) are certainly induced by my
    using Regexps scarcely and only those, that I already master for some
    time.

    In which way do you consider the O'Reilly book superior to other
    publications or, to simplify, the man-pages to egrep, regex (7) or the
    RDoc to Regexp, my principal though rarely consulted sources of
    information?

    There is also a nice programm called "Regex Coach" which allows to watch
    a regex engine match. This is really helpful for learning. Runs under
    WINE as well.

    http://weitz.de/regex-coach/

    I will consider this. Paul Lutus put a “Regular Expression Laboratory”
    on his web-site (what did he *not* put on his web-site, actually?). It
    is simpler with less options than the “Regex-Coach”: http://arachnoid.com/regex_lab/index.html


    It solves my current problem, though.

    But note the form above does _not_ do word splitting. For that you need
    the other one.

    Yes. Word-splitting was not in the original “requirement”; I only want
    to ensure that a text fits into a laterally limited space... like, let's
    say, for a text-mode spreadsheet program, if line-breaks are activated
    for a cell. Of all choices.

    Cheerio.


    Nice holidays all, happy new year, too, within the limits of the
    televised joy.

    Same to you!

    Kind regards

    robert



    --
    GnuPG brainpoolP512r1/5C2A258D 2015-10-02 [expires: 2017-10-01]
    sub brainpoolP512r1/53461AFA 2015-10-02 [expires: 2017-10-01]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Michael Uplawski on Sat Dec 24 18:37:48 2016
    On 22.12.2016 15:34, Michael Uplawski wrote:
    On Wed, 21 Dec 2016 19:02:29 +0100,
    Robert Klemme <shortcutter@googlemail.com> wrote:

    You should really get familiar with regular expressions. One book I
    usually recommend is "Mastering Regular Expressions" (O'Reilly).

    You are probably right. The difficulties that I have with String.scan
    (and functions documented in a similar way) are certainly induced by my
    using Regexps scarcely and only those, that I already master for some
    time.

    The knowledge needed about regular expressions and Ruby's dialect of
    them is obviously vast compared to the basic mechanics of these methods.
    So the documentation has to leave that "bit" out.

    In which way do you consider the O'Reilly book superior to other
    publications or, to simplify, the man-pages to egrep, regex (7) or the
    RDoc to Regexp, my principal though rarely consulted sources of
    information?

    The book is not a textbook on formal languages (like the classic
    dragonbook) but rather a pragmatic's guide to how modern regex engines
    work, why they work that way and what the side effects of the fact are
    that most are NFA today (famous exception: sed). It explains how to
    write expressions to efficiently match and shows techniques to tackle
    specific problems.

    https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools

    Manpages of grep also touch regular expressions very briefly only and do
    not explain the concept.

    I will consider this. Paul Lutus put a “Regular Expression Laboratory”
    on his web-site (what did he *not* put on his web-site, actually?). It
    is simpler with less options than the “Regex-Coach”: http://arachnoid.com/regex_lab/index.html

    But there is a fundamental difference: that tool will only highlight
    matches - something that you can do with IRB or grep (with match
    coloring) or any other tool / language. But with the Coach you can
    watch how the engine (a NFA btw.) actually matches internally. You see
    the backtracking that will occur in certain circumstances etc.

    Cheerio.

    ... Mrs. Sophie!

    robert

    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert L.@21:1/5 to Michael Uplawski on Sun Jan 15 14:10:53 2017
    On 12/19/2016, Michael Uplawski wrote:

    arr = str.split(/\s/)

    arr = str.split

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)