in reply to Masking part of a string
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Masking part of a string
by johngg (Canon) on Jun 27, 2007 at 14:36 UTC | |
by ikegami (Patriarch) on Jun 27, 2007 at 15:02 UTC | |
by johngg (Canon) on Jun 27, 2007 at 16:20 UTC |