splitting lines

B

Bob Smith

hi
how to split text into lines?
I have in a db whitespace delimited words forming chapters in one field,
and I want to nicely put it on the page with max 30 or so characters per
paragraph, after that line breaks should occur,
but I can't figure out the regexp for doing it,
any help much appreciated. Thank you
/G
 
B

Bigus

Bob Smith said:
hi
how to split text into lines?
I have in a db whitespace delimited words forming chapters in one field,
and I want to nicely put it on the page with max 30 or so characters per
paragraph, after that line breaks should occur,

I take it you mean 30 characters per line rather than paragraph?

If so, try something like this:

@lines = $dbtext =~ /(.{30}\S*)/g;
foreach(@lines)
{
$_ =~ s/^\s+//;
print "$_\n";
}

Bigus
 
D

D Borland

Bigus said:
I take it you mean 30 characters per line rather than paragraph?

If so, try something like this:

@lines = $dbtext =~ /(.{30}\S*)/g;
foreach(@lines)
{
$_ =~ s/^\s+//;

can't that be typed just as s/^\s+//; not $_ =~ s^\s+//;
 
T

Trent Curry

D said:
can't that be typed just as s/^\s+//; not $_ =~ s^\s+//;

Yes it can, since s/ (a substitution op) with no lhs implies using $_;

--
Trent Curry

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1)!eg
;print(reverse("$s")."\n");'
 
E

Eric Bohlman

Bob Smith said:
hi
how to split text into lines?
I have in a db whitespace delimited words forming chapters in one field,
and I want to nicely put it on the page with max 30 or so characters per
paragraph, after that line breaks should occur,
but I can't figure out the regexp for doing it,

You're better off using Text::Format, or Text::Autoformat, or a related
module for this rather than trying to create a regexp. The authors of
those modules have already dealt with a number of "gotchas" that occur in
text formatting.
 
M

Mala Ananthamurthy

Bob Smith said:
hi
how to split text into lines?
I have in a db whitespace delimited words forming chapters in one field,
and I want to nicely put it on the page with max 30 or so characters per
paragraph, after that line breaks should occur,
but I can't figure out the regexp for doing it,
any help much appreciated. Thank you
/G

$outputline =~ s/(.{1,30})/$1\n/gs;

print "$outputline";
 
M

Mala Ananthamurthy

Bob Smith said:
hi
how to split text into lines?
I have in a db whitespace delimited words forming chapters in one field,
and I want to nicely put it on the page with max 30 or so characters per
paragraph, after that line breaks should occur,
but I can't figure out the regexp for doing it,
any help much appreciated. Thank you
/G

#This one looks for space character
$outputline =~ s/(.{1,30})\s+/$1\n/gs;

print "$outputline";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top