f



recompose strings of limited length

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]

0
Michael
12/19/2016 4:19:53 PM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

5 Replies
609 Views

Similar Articles

[PageSpeed] 12

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 =3D 10
strings =3D str.scan /.{1,#{max_len}}/

> -----------
> me@here:/tmp$ ./test.rb "Ein K=C3=B6nig mit Krone ist besser als ohne" =
15
> Ein K=C3=B6nig mit
> Krone ist
> besser als
> ohne
> -----------

You never mentioned that you want word splitting.

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

It seems your last two strings should be one.

Cheers

	robert

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

0
Robert
12/20/2016 6:16:52 PM
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]

0
Michael
12/21/2016 9:55:49 AM
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/
0
Robert
12/21/2016 6:02:29 PM
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]

0
Michael
12/22/2016 2:34:00 PM
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=20
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=20
dragonbook) but rather a pragmatic's guide to how modern regex engines=20
work, why they work that way and what the side effects of the fact are=20
that most are NFA today (famous exception: sed).  It explains how to=20
write expressions to efficiently match and shows techniques to tackle=20
specific problems.

https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tool=
s

Manpages of grep also touch regular expressions very briefly only and do =

not explain the concept.

> I will consider this. Paul Lutus put a =E2=80=9CRegular Expression Labo=
ratory=E2=80=9D
> on his web-site (what did he *not* put on his web-site, actually?). It
> is simpler with less options than the =E2=80=9CRegex-Coach=E2=80=9D:
> http://arachnoid.com/regex_lab/index.html

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

> Cheerio.

=2E.. Mrs. Sophie!

	robert

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

0
Robert
12/24/2016 5:37:48 PM
Reply: