in reply to Replacing letters in one string based on another

Try this:
use strict; use warnings; my $str1 = "ABCXXFGH"; my $str2 = "XXCDEFGH"; my $str3 = "XXCXXFGH"; my ($str_to_retain) = $str2 =~ /(\w\w\w).*/; my ($replacement) = $str1 =~ /\w\w\w(.*)/; my $replaced_str = $str_to_retain.$replacement; print "$str3\n$replaced_str\n";

Replies are listed 'Best First'.
Re^2: Replacing letters in one string based on another
by AnomalousMonk (Archbishop) on Sep 16, 2010 at 23:10 UTC

    But BrowserUk's approach will work for any two strings (Update: assuming, as GrandFather points out, that the two strings are identical except for the Xs), while your solution depends on the particular structure of the strings given in the example of the OP.