Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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
[Corion]: Whee! Germanys Grand Central Airport (for Berlin, re-scheduled for the thrid or fourth time, to open now in 2018) has just fired their technical lead
[ambrus]: Corion: Is their budget estimate four times the initial estimate yet? Was the construction late by six months already three months after they started?
[marto]: I'll believe it when I see it. That's been due for many years now :P
[marto]: oddly the last time I was there I saw a zeppelin in flight for the first time
[Corion]: ambrus: I don't know how far the budget estimate has been overrun yet
[Corion]: marto: Heh ;) No, you were likely at Tegel or Schönefeld, the new one ("BER") will be somewhat outside of Berlin ;)
[marto]: Corion Schoenfeld, but my friend has been telling me about this new airport for about 7-8 years now :P
[marto]: I doubt it even exists as a building site :P

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-02-23 11:58 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (346 votes). Check out past polls.