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

Re^2: Word wrapping using a regex.

by Additude (Initiate)
on Nov 14, 2012 at 14:26 UTC ( #1003814=note: print w/replies, xml ) Need Help??

in reply to Re: Word wrapping using a regex.
in thread Word wrapping using a regex.

I think to make this work we need to add another line at the end to tell it what to do with the last of the string that is less than $wrap.
sub wrap { my ($s, $wrap) = @_; my (@lines, $line); while (length $s > $wrap) { ### Take one extra character so we can see if ### we're wrapping inside a word or not $line = substr($s, 0, $wrap+1); $line =~ s/\s+(\S*)$//; push @lines, $line; ### Add any word fragments back on $s = $1 . substr($s, $wrap); $s =~ s/^\s+//; } push @lines, $s; ## Adds the the last of the $s string to @lines return join "\n", @lines; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003814]
[chacham]: I'm sure this is old, but i just came across it: If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl programs.
[LanX]: lol
[Corion]: Woo hoo! A new Auralnauts episode!
LanX mean unfair and funny
[LanX]: but most java code looks like from one million monkey using eclipse. ..
[Eily]: ah yes, perl's motto, There Is More Than One Monkey Typing It

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2018-04-23 16:06 GMT
Find Nodes?
    Voting Booth?