#!/usr/bin/perl use warnings; use strict; my $n = shift; $n--; my $test = 'A huge string separated by lots and lots of words that I\'d like to break up into n shorter strings of length y'; my @words = split /\s+/, $test; #First method, doesn't pickup the leftovers for(my $i = 0; $i+$n < $#words; $i += $n+1) { print join(' ', @words[$i..$i+$n]), "\n"; } print "\n\n"; #second method, picks up the leftovers my $j = 0; while($j+$n < $#words) { print join(' ', @words[$j..$j+$n]), "\n"; $j += $n+1; } print join(' ', @words[$j..$#words]), "\n";