use warnings; use strict; my $str = 'dog dog dog dog dog'; print join " " => ( split /\s+/, $str )[ 0 .. 2 ];