How to add a new line after X number of characters in Perl?

I was wondering how to add a newline character (i.e. /n or <br> ) after the X number of characters.

For example, suppose I have a perl variable $ message = "aaaaabbbbbccccccdd". I want to add a newline character after every 5 characters of this variable. Therefore, when I print a variable in html, it displays:

 aaaaa bbbbb ccccc dd 

What is the best way to do this? I was told to use the substr or count function, but I'm not sure how to do this. Any help would be appreciated. Thanks!

+6
perl
source share
9 answers

An even shorter option.

 $m = "aaaaabbbbbcccccdd"; $m =~ s/(.{1,5})/$1\n/gs; print $m; 

Outputs:

 aaaaa bbbbb ccccc dd 

Of course, I think my version is best presented so far.;)

+9
source share

I heard that the most efficient way is to use unpacking:

 say for unpack "(A5)*", "012345678901234567890123456879" 

Output:

 01234 56789 01234 56789 01234 56879 
+4
source share

Based on Massa's answer, I would do it as follows:

 $message = join("\n", unpack('(A5)*', $message )) 

running it

 $ perl use strict; use warnings; my $message = "aaaaabbbbbcccccdd"; $message = join("\n", unpack("(A5)*", $message)); print $message; ^D aaaaa bbbbb ccccc dd 

Replace "\ n" with what you want to actually complete each line (for example, "\ <br> \ n".)

+4
source share

There are many ways to do the same thing in perl :-)

One of them may be:

 $message = "aaaaabbbbbcccccdd"; $splitmessage = join ("\n", ( $message =~ /.{1,5}/gs )); print $splitmessage, "\n"; 

Output:

 aaaaa bbbbb ccccc dd 
+3
source share

Since you are trying to wrap text, I would look at something like Text :: Wrap

0
source share

The unpacking method is probably the most effective if a little dumb. The regex method is probably the most Perlish way to do this. But since this is Perl, there is more than one way to do this, so there are a few more interesting ways to do this:

using List::MoreUtils::natatime ("n-at-a-time"). This method, of course, wildly wasteful memory, creating a scalar for each character in the string.

 use List::MoreUtils qw(natatime); my $in = "aaaaabbbbbcccccdd"; my $out = ''; my $it = natatime 5, split //, $in; while(my @chars = $it->()) { $out .= $_ for @chars; $out .= "\n"; } 

using the substr argument substr for splicing in new lines, working from the end: (you must work from the end, because otherwise further offsets no longer line up after you start adding new lines, and also work from the end means what do you calculate length $in at the start of the loop without using an extra variable)

 for(my $i = length($in) - length($in) % 5; $i; $i -= 5) { substr($in, $i, 0, "\n"); } 

if you want to keep the input variable as is, you can pre-calculate all offsets and extract them with substr

 foreach (map $_ * 5, 0 .. int(length($in) / 5)) { $out .= substr($in, $_, 5) . "\n"; } 

perhaps the most concise way to use substr is to use substitution and concatenate the return value:

 $out .= substr($in, 0, 5, '') . "\n" while $in; 
0
source share
 substr($string, 0, 5); 

A couple with some variables:

 $x = 0; $newstring = ''; while(length($string)<$x){ $newstring = $newstring + substr($string, $x, ($x+5)) + '\n'; $x = $x + 5; } 
-one
source share

Just pass the string through this regex:

 =~ s/([^\n]{5})/$1\n/g 

and everything should be fine.

If you really have a random string of random characters, but text - you can use the Text :: Wrap module instead.

-one
source share
 echo 123456789abcd|perl -ne'print "$1\n" while s/(^.{5})//;print;' 
-2
source share

All Articles