Augustina's Technological Blog

Technology, Perl, Linux, and a Woman's perspective on the FOSS community

Inserting Line Breaks Into Text

with 2 comments

This will force a line break into every 10th character.

$count = 0;
$_ = '1PATonthebackbackpatonthefoot2PAT3PAT4PAT';

s{
.
}{
if ( (++$count %10) == 0) {
$& . "\n";
} else {
$&;
}
}gex;

print;
print "\n"

$_ can also be a specified scalar

$count = 0;
$string = '1PATonthebackbackpatonthefoot2PAT3PAT4PAT';
#$_ = $string; < ---another option

$string =~ s{
\S
}{
if ( (++$count %10) == 0) {
$& . "\n";
} else {
$&;
}
}gex;

print $string;
print "\n"

This was courtesy of the Perl Cookbook!

Advertisements

Written by missaugustina

April 6, 2007 at 6:04 pm

Posted in Perl, Programming

2 Responses

Subscribe to comments with RSS.

  1. As a Newbie, I am always researching online for articles that can help me get further ahead. Thanks a million!

    Danna Panama

    February 4, 2010 at 4:56 am

  2. I am glad I found your website on bing. Thank you for the sensible critique. Me and my husband were just preparing to do some research about this. I am very happy to see such good information being shared for free out there.
    Best wishes,
    Doyle from Paterson city

    Ansley

    February 24, 2010 at 7:04 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: