my $line = " 0 10 9 4 1 0 0 0 2 2 1 1 0"; my @config = grep /\S/, split / /, $line; # or even my @config = grep /\A\d+\z/, split / /, $line; print join(", ", @config), "\n";