in reply to More efficient way for this pattern match?

See also this thread for a very similar, though not identical, problem. Also, Hello! to your perl teacher.

#! /usr/bin/perl sub ano1 { my ($str1, $str2) = @_; my ($last) = $str2 =~ /.*(\w)/; tr/\0-/-=/, s/=(?=\W*(\w?))/ $1 || $last /ge for my $answer = $str1 =~ tr/A-Z/\0/r ^ $str2; $answer; } sub ano2 { my ($x, $y) = @_; $y &= ($y =~ tr/-\0-\377/\0\377/r) | ($x =~ tr/-\0-\377/\377\0/r); $y =~ s/(\0+)(.)/$2 x (1 + length $1)/eg; $y =~ s/(.)(\0+)$/$1 x (1 + length $2)/er; } chomp (our ($str1, $str2) = <DATA>); use Benchmark 'cmpthese'; cmpthese -5, { ano1 => q( ano1 $str1, $str2 ), ano2 => q( ano2 $str1, $str2 ), }; __DATA__ MCCAALAPPMAATVGPESIWLWIGTIGMTLGTLYFVGRGRGVRDRKMQEFYIITIFITTIAAAMYFAMAT +GFGVT-------------EVMVG----DE---ALTIYWARYADWLFTTPLLLLDLSLLAGANRN----T +IATLIG-LDVFMIG---T---GAIAALSST-PGTRIAWWAIST--GALL--ALLYVLVGTLSENARNRA +PEVA--SLFGRLRNLVIALWFLYPVVWILGT---EGTFGILP--LYWETAAFMVLDLSAKVGFGVILLQ +SRSVLERVATPTAAPT --OOOOOOOOOOOOOOOOMMMMMMMMMMMMMMMMMMMMMIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMM +OOOOO-------------OOOOO----OO---OOOOMMMMMMMMMMMMMMMMMMMMMIIIIIII----M +MMMMMM-MMMMMMM---M---MMMMMMOOO-OOOOMMMMMMMM--MMMM--MMMMMMMMMMIIIIIIII +IIII--IIIIMMMMMMMMMMMMMMMMMMMMO---OOO-OOOO--OOOMMMMMMMMMMMMMMMMMMMMMI +IIIIIIIIIIII----

Replies are listed 'Best First'.
Re^2: More efficient way for this pattern match?
by Anonymous Monk on Apr 15, 2015 at 12:07 UTC

    Sigh, this is too crazy. I give up, and I'll go back to golf!

    This runs faster as a one-liner than as multiple lines. :(

    Also tr/A-Z/\0/r is faster than tr/-/\0/cr It makes no sense :(

    sub ano1 { (pop ^ pop =~ tr/A-Z/\0/r) =~ tr/\0-/-=/r =~ s/(=+(\w))/$2 +x length $1/ger =~ s/((.)=+)$/$2 x length $1/er }