# build a regex to match just your keys my $removers = "^(?:" . join ("|", map { quotemeta $_ } @text_remove) . ") "; # print $removers out if you're not getting what you expect # strip the patterns from the beginning of each line. @xlate_data = map { s/$remove//; $_; } @xlate_data; #### # use whatever delimiter makes sense here. I'm assuming # you don't have newlines at the ends of the elements already. my $full_string = join "\n", @xlate_data; # since you know all the [ are gone from # the elements that matched, and that they will be present # in the ones that didn't match, a newline followed by # something other than a [ is a line you want to join. # so strip \n's not followed by a [ $full_string =~ s/\n([^[])/$1/g; # back to an array @xlate_data = split/\n/, $full_string;