sub foo # not sure what you'd call this { my $pat = shift; my @r; while (@_) { my $x = shift; push @r, $x; $x =~ /$pat/ and $r[-1] .= shift; } @r } my @new = foo( qr/string1/, @array );