in reply to array element question
In situations like this, I find it a little more natural to shift off the elements, rather than iterate using a counter. But it means you have to take measures to keep from destroying the source, if that would matter. Putting it all in a subroutine can provide this measure of safety.
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 );
|
|---|