foreach my $name ( @names ) { my @n; foreach ( split ' ', $name ) { # Go through each item, push'ing onto @n } $name = join ' ', @n; }