in reply to How to make this substitutions without splitting the strings?
(NB: It's not really necessary to post a reeeeealy looooong string to convince us you're dealing with long strings. We're prepared to take your word. Shorter example strings would seem to have been sufficient.)
Insofar as I understand your problem, this is the classic, idiomatic Perlish solution. (I don't know if you really need your $str1 string transformed also; I do it essentially to support the example.) This approach assumes that $s1 and $s2 are always the same length, and also that the null character \x00 is never a valid part of the $s1 or $s2 string.
c:\@Work\Perl\monks>perl -wMstrict -le "my $s1 = '---DAAAGLRG--G--G-P-LT-IGGY'; my $s2 = '...XXXXXXXX..X..X.X.XX.X..X'; print qq{'$s1'}; print qq{'$s2'}; ;; (my $mask = $s1) =~ tr{-\x00-,.-\xff}{\x00\xff}; my $t1 = $s1 & $mask; my $t2 = $s2 & $mask; $t1 =~ tr{\x00}''d; $t2 =~ tr{\x00}''d; ;; print qq{'$t1'}; print qq{'$t2'}; " '---DAAAGLRG--G--G-P-LT-IGGY' '...XXXXXXXX..X..X.X.XX.X..X' 'DAAAGLRGGGPLTIGGY' 'XXXXXXXXXXXXXX..X'
Update: Replaced s/// with tr/// in masked character removal step: $t1 =~ tr{\x00}''d; instead of $t1 =~ s{ \x00+ }''xmsg;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to make this substitutions without splitting the strings?
by Anonymous Monk on Jul 28, 2014 at 12:02 UTC | |
by AnomalousMonk (Archbishop) on Jul 28, 2014 at 13:56 UTC |