use warnings; use strict; for my $i ( " ab ", " ab ", " abc ", " abcd ", " abcde ", " abcdef ", " abcdefg " ) { #### $i =~ /\A(\s*)(.*?)(\s*)\z/s or die; my $o; if (length($1) + length($2) < 6) { $o = substr($i, 0, 6); } elsif (length($2) < 6) { $o = substr($1 . $2, -6); } else { $o = substr($2, 0, 6); } #### printf "%-15s%s", qq("$i"), qq( => "$o"\n); } __END__