#! perl -slw use strict; for my $n ( 4 , 34, 70 ) { print "\n$n"; my $pos = tell DATA; while( ) { chomp; printf "\t'%s'\n", join "'+\n\t'", m[(.{1,$n}(?: |$)|.{$n})]g; } seek DATA, $pos, 0; } __DATA__ Here is one big long string which needs to have some formatting done to it before I can use it for input in a separate program. I would like it to be formatted exactly as follows (except for the exact line length)