Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Breaking the lines

by Ben Win Lue (Friar)
on Jul 19, 2007 at 08:01 UTC ( #627462=perlquestion: print w/replies, xml ) Need Help??
Ben Win Lue has asked for the wisdom of the Perl Monks concerning the following question:

Good morrow brethren

I'm sure this question has been asked before, but I don't find it.

How do I break a string without line breaks into lines of a specific maximum length?
The break should be placed at the latest white space before reaching the maximum line length.

A regular exression? I can't think of a way.

use Text::something??

or just the hard way, like I would do it in C???

Thank you for your attention

Replies are listed 'Best First'.
Re: Breaking the lines
by Corion (Pope) on Jul 19, 2007 at 08:17 UTC

    Text::Wrap seems to do what you want. I think the following regular expression will also do "what you want", as long as there are no words longer than your maximum line lenght:

    use strict; my $line_length = 10; my $line = '123 1234 12345 123456 1234567'; my @lines = ($line =~ /(.{1,$line_length}(?:\s|$))/g); print "-" x $line_length,"\n"; print "$_\n" for @lines;

    ... but I have only cursory tested that idea. I think it will fail for blanks that fall on the last column. Maybe you can fix that by allowing $line_length to be one larger than what fits onto the line.

      Thanks a lot!

      Works really good. Now it's my job to spend some spare time and understand the regex!

Re: Breaking the lines
by cdarke (Prior) on Jul 19, 2007 at 09:13 UTC
    formline with a format would probably do it.
    use strict; my $str="Reports that say that something hasn't ". "happened are always interesting to me " . "because as we know, there are known knowns; ". "there are things we know we know. We also ". "know there are known unknowns; that is to ". "say we know there are some things we do not ". "know. But there are also unknown unknowns --". " the ones we don't know we don't know."; my $picture = '^<<<<<<<<<<<<<<<<<<<< ~~'."\n"; formline ($picture, $str); print "$^A\n";

    Adjust the picture variable to suite your needs. If the number of chars is variable, it is fairly simple to construct the format using the 'x' operator
Re: Breaking the lines
by Anno (Deacon) on Jul 19, 2007 at 10:21 UTC
    A regular exression? I can't think of a way.

    You mean a substitution. Only the fist half of a substitution is a regular expression.

    It's not that hard.

    my $l_max = 30; $str =~ s/(.{0,$l_max}) /$1\n/g;
    Mind you, Text::Wrap does a far better job than this simple substitution. I'm not recommending to use it instead.


Re: Breaking the lines
by GrandFather (Sage) on Jul 19, 2007 at 09:37 UTC
Re: Breaking the lines
by schwern (Scribe) on Jul 19, 2007 at 20:29 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://627462]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2017-10-20 13:49 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (263 votes). Check out past polls.