http://qs321.pair.com?node_id=623720


in reply to Masking part of a string

I could not resist giving a functional and expensive solution to the problem with pairwise function from List::MoreUtils.

my $mask = '001111100'; my $str = 'AGACGAGTA'; my @mask = split( '', $mask ); my @str = split( '', $str ); my $masked = join '', pairwise { $a ? 'x' : $b } @mask, @str; print "Masked: $masked\n";
And now using regexes:
my $mask = '001111100'; my $str = 'AGACGAGTA'; my $masked = ''; while ( $mask =~ /\G(.)/gc ) { # walk the mask my $bit = $1; if ( $str =~ /\G(.)/gc ) { # walk the string to be masked $masked .= $bit ? 'x' : $1; } } print "Masked: $masked\n";

Probably it could be improved in space if mask is represented as a bit vector and there is an iterator to walk the mask just like $mask =~ /\G(.)/gc is doing.