in reply to printing 20 characters in a line.

Yet another option using regular expression:

while ($str =~ /((\s*\d+){1,20})/g) { print OUT "$1\n"; }

update: get rid of leading spaces

while ($str =~ /\s*((\s*\d+){1,20})/g) { print "$1\n"; }

update2: which you can do as a one-liner

print "$1\n" while ($str =~ /\s*((\s*\d+){1,20})/g);

Replies are listed 'Best First'.
Re^2: printing 20 characters in a line.
by johngg (Canon) on Nov 14, 2008 at 14:44 UTC

    To my eye the regex reads better if you start the memory group with a set of digits (thus removing the worry about leading spaces) and then follow with grouped spaces and digits with a quantifier of 0 to 19. That avoids the adjacent \s* tokens which might be a little confusing at first glance. Also, you don't need the parentheses around the match.

    print qq{$1\n} while $str =~ m{(\d+(?:\s+\d+){0,19})}g;

    I hope this is of interest.

    Cheers,

    JohnGG