$ perl -Mstrict -MData::Dumper -e ' my @words=("",",","abcd","efgh","","","jklm","nopq",""); my $combined; my @combinations; for my $word (@words){ if ($word){ $combined .=$word; } else{ push @combinations, $combined if $combined; $combined=""; } } @words=@combinations; print Dumper\@words;' $VAR1 = [ ',abcdefgh', 'jklmnopq' ];