my @other_stuff = split (' ', $words); my @pairs = $string =~ /..?/sg; my @stuff = grep /\B$pairs[0]|\B$pairs[1]|\B$pairs[2]/, @other_stuff; # this assumes I know the word length. # but actually I would not know. # so this is not ok and needs to be fixed! for (@stuff) { print $_ . " "; print OUTPUT $_ . " "; } close OUTPUT; exit;