[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Cut a string each 80 character

Guillaume Loader

3/5/2009 2:06:00 AM

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 :)
--
Posted via http://www.ruby-....

8 Answers

Guillaume Loader

3/5/2009 2:07:00 AM

0

Ooops!
Replace "each" by "every"

Sorry!
--
Posted via http://www.ruby-....

Gennady Bystritsky

3/5/2009 2:41:00 AM

0

PiAtLS0tLU9yaWdpbmFsIE1lc3NhZ2UtLS0tLQ0KPiBGcm9tOiBwaWNwaWM3MkBob3RtYWlsLmNv
bSBbbWFpbHRvOnBpY3BpYzcyQGhvdG1haWwuY29tXQ0KPiBTZW50OiBXZWRuZXNkYXksIE1hcmNo
IDA0LCAyMDA5IDY6MDYgUE0NCj4gVG86IHJ1YnktdGFsayBNTA0KPiBTdWJqZWN0OiBDdXQgYSBz
dHJpbmcgZWFjaCA4MCBjaGFyYWN0ZXINCj4gDQo+IEhlbGxvIGV2ZXJ5b25lIQ0KPiANCj4gSSB3
YW50IHRvIGRvIGN1dCBhIHN0cmluZyBlYWNoIDgwIGNoYXJhY3Rlci4gRG8geW91IGtub3cgaG93
IHRvIGRvPw0KPiANCj4gRm9yIGluc3RhbmNlIDoNCj4gc3RyaW5nID0gImpkc2tqIGZqa2pqa2wN
Cj4gZGpza2ZsamtzbGRqZmtqZGtsamtzbGprZGpma2Rsc2pma2xzamRrZmxkc2pka2ZsamtsZGpr
c2pma2xqa2RsZmprZHNsamsNCj4gbGpmZGtsc2tqa2RmbGRmbGRmamtkZmprbGRmc2tqbGRmc2tq
bGRma2psZGZza2psc2Rma2psZGZza2xqZGZza2psZGtqbGQNCj4gZmtqZGZrbGxrZGYiDQoNCnN0
cmluZy5zY2FuKCVyey57MSw4MH19KQ0KDQpvcg0KDQpuID0gODANCnN0cmluZy5zY2FuKCVyey57
MSwje259fSkNCg0KPiANCj4gRm9yIHRoaXMgc3RyaW5nLCBJIHdhbnQgdG8gYWRkICA8YnIgLz4g
ZWFjaCA4MCBjaGFyYWN0ZXIgZm9yIHRoZSB0aGlyZA0KPiB3b3JkLg0KDQpzdHJpbmcuc2Nhbigl
cnsuezEsODB9fSkubWFwIHsgfF9jaHVua3wNCiAgX2NodW5rICsgJzxiciAvPicNCn0NCg0KTWFr
ZSBjb3JyZXNwb25kaW5nIGFkanVzdG1lbnRzIGlmIHlvdSB3YW50IHRvIGFwcGx5IGl0IHRvIGEg
cGFydGljdWxhciB3b3JkIG9mIHlvdXIgc3RyaW5nIChlLmcuLCBkbyBzdGlybmcuc3BsaXQuc2xp
Y2UoMikgZm9yIHRoZSB0aGlyZCB3b3JkKS4NCg0KSG9wZSBpdCBoZWxwcywNCkdlbm5hZHkuDQo=

Kyle Schmitt

3/5/2009 2:45:00 AM

0

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

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)

-lim-

3/5/2009 5:53:00 AM

0

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

Isn't that pretty much the same as:

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

leo.

Gennady Bystritsky

3/5/2009 8:31:00 AM

0

> -----Original Message-----
> From: Leo [mailto:minilith@gmail.com]
> Sent: Wednesday, March 04, 2009 9:53 PM
> To: ruby-talk ML
> Subject: Re: Cut a string each 80 character
>=20
> > string.scan(%r{.{1,80}}).map { |_chunk|
> > =A0 _chunk + '<br />'
> > }
>=20
> Isn't that pretty much the same as:
>=20
> string.gsub(%r{.{1,80}}, "\\0<br />")
>=20
> leo.

Almost. The latter produces a string with "<br />" after every 80 chars, wh=
ile 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 eve=
n further by passing them through some other methods (on the other hand, gr=
ep with a block may be used for that as well).

The latter may be faster, though.=20

Thanks for bringing it up,
Gennady.


lasitha

3/5/2009 11:33:00 AM

0

On Thu, Mar 5, 2009 at 2:00 PM, Gennady Bystritsky
<Gennady.Bystritsky@quest.com> wrote:
>> -----Original Message-----
>> From: Leo [mailto:minilith@gmail.com]
>> Sent: Wednesday, March 04, 2009 9:53 PM
>>
>> > string.scan(%r{.{1,80}}).map { |_chunk|
>> > =A0 _chunk + '<br />'
>> > }
>>
>> Isn't that pretty much the same as:
>>
>> string.gsub(%r{.{1,80}}, "\\0<br />")
>
> [...]
> The latter may be faster, though.

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

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.

Robert Klemme

3/5/2009 12:32:00 PM

0

On 05.03.2009 12:32, lasitha wrote:
> On Thu, Mar 5, 2009 at 2:00 PM, Gennady Bystritsky
> <Gennady.Bystritsky@quest.com> wrote:
>>> -----Original Message-----
>>> From: Leo [mailto:minilith@gmail.com]
>>> Sent: Wednesday, March 04, 2009 9:53 PM
>>>
>>>> string.scan(%r{.{1,80}}).map { |_chunk|
>>>> _chunk + '<br />'
>>>> }
>>> Isn't that pretty much the same as:
>>>
>>> string.gsub(%r{.{1,80}}, "\\0<br />")
>> [...]
>> The latter may be faster, though.
>
> Doesn't appear to be.
> http://gist.github...
>
> 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)


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
require 'benchmark'

S = ('0123456789' * (8 * 1_000)).freeze
R = '<br/>'.freeze
GS = "\\&#{R}".freeze
SANE = '<br/>0123456789'.freeze
REP = 500
DELTA = 80 + R.length

def with_scan s
s = s.scan(/.{80}/).join(R)
raise 'with_scan failed' unless s[-85..-71] == SANE
s
end

def with_gsub s
s = s.gsub(/.{80}/, GS).chomp!(R)
raise 'with_gsub failed' unless s[-85..-71] == SANE
s
end

def with_insert s
s = s.dup
i = 80
while i < s.length
s.insert i, R
i += DELTA
end
raise 'with_insert failed' unless s[-85..-71] == SANE
s
end


Benchmark.bmbm do |bm|
bm.report('with_scan ') { REP.times { with_scan S }}
bm.report('with_gsub ') { REP.times { with_gsub S }}
bm.report('with_insert') { REP.times { with_insert S }}
end

lasitha

3/5/2009 4:03:00 PM

0

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...
>>
>
>
> There is an inconsistency in your test: the gsub version creates the seco=
nd
> argument to gsub over and over again. =A0You should rather define a const=
ant
> for that as well. =A0You could do that as well for the sanity check strin=
g.

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.