Forum: Ruby Cut a string each 80 character

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-03-05 03:06
Hello everyone!

I want to do cut a string each 80 character. Do you know how to do?

For instance :
string = "jdskj fjkjjkl
djskfljksldjfkjdkljksljkdjfkdlsjfklsjdkfldsjdkfljkldjksjfkljkdlfjkdsljkljfdklskjkdfldfldfjkdfjkldfskjldfskjldfkjldfskjlsdfkjldfskljdfskjldkjldfkjdfkllkdf"

For this string, I want to add  <br /> each 80 character for the third
word.


Thank you :)
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-03-05 03:08
Ooops!
Replace "each" by "every"

Sorry!
397f61cc4458e13157b4facf72325e5f?d=identicon&s=25 Gennady Bystritsky (Guest)
on 2009-03-05 03:43
(Received via mailing list)
> For instance :
> string = "jdskj fjkjjkl
> djskfljksldjfkjdkljksljkdjfkdlsjfklsjdkfldsjdkfljkldjksjfkljkdlfjkdsljk
> ljfdklskjkdfldfldfjkdfjkldfskjldfskjldfkjldfskjlsdfkjldfskljdfskjldkjld
> fkjdfkllkdf"

string.scan(%r{.{1,80}})

or

n = 80
string.scan(%r{.{1,#{n}})

>
> For this string, I want to add  <br /> each 80 character for the third
> word.

string.scan(%r{.{1,80}}).map { |_chunk|
  _chunk + '<br />'
}

Make corresponding adjustments if you want to apply it to a particular
word of your string (e.g., do stirng.split.slice(2) for the third word).

Hope it helps,
Gennady.
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2009-03-05 03:47
(Received via mailing list)
On Wed, Mar 4, 2009 at 8:06 PM, Guillaume Loader <picpic72@hotmail.com>
wrote:
> Ooops!
> Replace "each" by "every"
>
> Sorry!
> - Show quoted text -
> --
> Posted via http://www.ruby-forum.com/.
>
>

I'm thinking you want scan.

string.scan(/.{8}/)


You may need to adjust the regex a little, but that should match 8
charicters at a time...
I think if the length isn't a multiple of 8 it may just ignore the
last bunch of chars... but you get the idea

edit the regex as needed.

PS: Scan is _awesome_ when you need to do some quick an dirty log
analysis
data=File.read(foo).scan(regex)
397f61cc4458e13157b4facf72325e5f?d=identicon&s=25 Gennady Bystritsky (Guest)
on 2009-03-05 04:26
(Received via mailing list)
> >
>
>
> You may need to adjust the regex a little, but that should match 8
> charicters at a time...
> I think if the length isn't a multiple of 8 it may just ignore the
> last bunch of chars... but you get the idea

For that not to be ignored, use {1,8} instead of just {8}. As a minor
note, OP wanted 80, not 8 ;-)

Gennady.
87ef5d1e14b148eb596433bc17ffe690?d=identicon&s=25 Leo (Guest)
on 2009-03-05 06:55
(Received via mailing list)
> string.scan(%r{.{1,80}}).map { |_chunk|
>   _chunk + '<br />'
> }

Isn't that pretty much the same as:

string.gsub(%r{.{1,80}}, "\\0<br />")

leo.
397f61cc4458e13157b4facf72325e5f?d=identicon&s=25 Gennady Bystritsky (Guest)
on 2009-03-05 09:33
(Received via mailing list)
> Isn't that pretty much the same as:
>
> string.gsub(%r{.{1,80}}, "\\0<br />")
>
> leo.

Almost. The latter produces a string with "<br />" after every 80 chars,
while the former returns an array of strings 86 chars long each. What is
more appropriate depends on a context. Say, you may want to decorate
chunks even further by passing them through some other methods (on the
other hand, grep with a block may be used for that as well).

The latter may be faster, though.

Thanks for bringing it up,
Gennady.
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-03-05 12:35
(Received via mailing list)
On Thu, Mar 5, 2009 at 2:00 PM, Gennady Bystritsky
<Gennady.Bystritsky@quest.com> wrote:
>> string.gsub(%r{.{1,80}}, "\\0<br />")
>
> [...]
> The latter may be faster, though.

Doesn't appear to be.
http://gist.github.com/74249

I threw in a primitive insert loop for comparison and was a little
surprised to find it considerably slower than both the others.

ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin8.11.1]
      user     system      total        real
with_scan    0.750000   0.040000   0.790000 (  0.851104)
with_gsub    0.840000   0.060000   0.900000 (  0.950050)
with_insert  2.000000   0.050000   2.050000 (  2.210962)

Solidarity,
lasitha.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-05 13:35
(Received via mailing list)
On 05.03.2009 12:32, lasitha wrote:
>>>
> ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin8.11.1]
>       user     system      total        real
> with_scan    0.750000   0.040000   0.790000 (  0.851104)
> with_gsub    0.840000   0.060000   0.900000 (  0.950050)
> with_insert  2.000000   0.050000   2.050000 (  2.210962)


There is an inconsistency in your test: the gsub version creates the
second argument to gsub over and over again.  You should rather define a
constant for that as well.  You could do that as well for the sanity
check string.

With the attached changed script this is what I get:

[robert@ora01 ~]$ allruby bm.rb
ruby 1.8.5 (2006-08-25) [i386-linux]
Rehearsal -----------------------------------------------
with_scan     1.770000   0.040000   1.810000 (  1.821903)
with_gsub     1.720000   0.010000   1.730000 (  1.750455)
with_insert   9.230000   0.000000   9.230000 (  9.228760)
------------------------------------- total: 12.770000sec

                   user     system      total        real
with_scan     1.730000   0.010000   1.740000 (  1.741069)
with_gsub     1.730000   0.010000   1.740000 (  1.741975)
with_insert   9.130000   0.000000   9.130000 (  9.130356)
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]
Rehearsal -----------------------------------------------
with_scan     1.040000   0.010000   1.050000 (  1.043332)
with_gsub     1.100000   0.010000   1.110000 (  1.120970)
with_insert   9.030000   0.000000   9.030000 (  9.077018)
------------------------------------- total: 11.190000sec

                   user     system      total        real
with_scan     1.030000   0.000000   1.030000 (  1.031019)
with_gsub     1.090000   0.010000   1.100000 (  1.105150)
with_insert   9.040000   0.000000   9.040000 (  9.049539)
[robert@ora01 ~]$

CentOS 5.2 in VM.

Kind regards

  robert
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-03-05 17:05
(Received via mailing list)
On Thu, Mar 5, 2009 at 6:03 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On 05.03.2009 12:32, lasitha wrote:
>>
>> Doesn't appear to be.
>> http://gist.github.com/74249
>>
>
>
> There is an inconsistency in your test: the gsub version creates the second
> argument to gsub over and over again.  You should rather define a constant
> for that as well.  You could do that as well for the sanity check string.

Yes, thank you for the pointers.  In this case the change didn't seem
to make any significant difference on my box, but the wisdom is
gratefully accepted.

Cheers,
lasitha.
This topic is locked and can not be replied to.