while(){ if ($_ eq $string_1) {$string .= $string_2; next;} elsif ($_ eq $string_2) {$string .= $string_1; next;} $string.= $_; }