You're dealing with characters (or octects), not bits, so you need to set 8 bits in the mask instead of 1 for each letter. substr is a good alternative to vec for doing that. Perl even has Bitwise String Operators (see perlop) that allow you to do bit operations on entire strings "at once".
my $str = 'AGACGAGTA'; my $mask = "\xFF" x length($str); substr($mask, $_, 1, "\x00") for 2..6; my $res = $str & $mask; $res =~ s/\x00/x/g; print("$res\n");
You can avoid the regexp call:
my $str = 'AGACGAGTA'; my $mask = "\xFF" x length($str); substr($mask, $_, 1, "\x00") for 2..6; my $x = 'x' x length($str); my $res = ($str & $mask) | ($x & ~$mask); print("$res\n");
Of course, you really don't need the mask at all.
my $str = 'AGACGAGTA'; my $res = $str; substr($res, $_, 1, "x") for 2..6; print("$res\n");
Update: Added the last two snippets.
In reply to Re: Masking part of a string
by ikegami
in thread Masking part of a string
by citromatik
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |