$string = "now is the time for all good men to come to the aid of their country while the gratuitously extralongwordwithmorethantwentycharactersexdtendson and on."; grep { push(@arr,substr($_,0,19)) } split (" ",$string); print join("\n",@arr); #### while ( $string =~ /(\S.{0,19})(?=\s|$)/g ) { # NB: "=~" here, rather than a simple "=" in original. push(@arr, $1); } for my $arr(@arr) { print $arr . "\n"; }