in reply to Re: A Quick Regex Question
in thread A Quick Regex Question

I like Gramp's solution: simple, elegant and efficient. Too often people automatically go for a regex when the good old tr operator is a better choice.

Due to my unfortunate golfing past, I can't restrain myself from noting that, if you are feeling in a silly mood, you could replace his simple and clear line:

print "Matched $_\n" if tr/pP// && tr/eE// && tr/rR// && tr/lL//;
with this silly one:
y&pP&&&&y&eE&&&&y&rR&&&&y&lL&&&&print"Matched $_\n";
Deparse confirming their equivalence:
# cat sensible.pl print "Matched $_\n" if tr/pP// && tr/eE// && tr/rR// && tr/lL//; # perl -MO=Deparse sensible.pl print "Matched $_\n" if tr/Pp// and tr/Ee// and tr/Rr// and tr/Ll//; sensible.pl syntax OK # cat silly.pl y&pP&&&&y&eE&&&&y&rR&&&&y&lL&&&&print"Matched $_\n"; # perl -MO=Deparse silly.pl print "Matched $_\n" if tr/Pp// and tr/Ee// and tr/Rr// and tr/Ll//; silly.pl syntax OK

Replies are listed 'Best First'.
Re^3: A Quick Regex Question
by GrandFather (Saint) on Oct 08, 2006 at 09:19 UTC

    Actually compared to the translate (counting) technique, for a case sensitive match McDarren's solution using the equivelent regex technique is slightly faster, but it's slightly slower if using a case insensitive match. Case sensitivity makes no significant difference to the translate.

    For the sake of code clarity I'd actually go with the multiple regex solution, but offered the translate solution because the technique gets forgotten about somewhat.


    DWIM is Perl's answer to Gödel