OceanPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Either nobody in the world has asked this before (unlikely), or my Google-fu is broken (more likely). But I come before the sage Perl monks to ask what seems to me to be an obvious question: According to PerlFAQ, it is apparently "almost" trivial to smart match against a vast swathe of regex patterns by smart matching an array. That's cool. But what if I want to sub out as a result ? For example, assume I've got the ghastly spaghetti-code below :
$cleanLine= $elem; $cleanLine =~ s/\/.*//g; $cleanLine =~ s/\?.*//; $cleanLine =~ s/;.*//; $cleanLine =~ s/;$//; $cleanLine =~ s/\.*//; etc. etc. ad infinitum
How can I make that all awesome, beautiful and able to process faster than the speed of light ? ;-) I assume smart matching arrays will have something to do with it, but if you know better, I'm all ears ! P.S. My example above shows all substitutions being to remove characters, but for extra bonus points, how would I implement different substitutions based on different patterns ?

Replies are listed 'Best First'.
Re: Smart match & substitution
by ikegami (Patriarch) on Feb 09, 2015 at 18:05 UTC

    it is apparently "almost" trivial to smart match against a vast swathe of regex patterns by smart matching an array.

    my $regexp = join '|', @patterns; $str =~ /$regexp/

    That's cool. But what if I want to sub out as a result ?

    $cleanLine =~ s/$_//g for @regexps;
Re: Smart match & substitution (matching isn't substitution)
by Anonymous Monk on Feb 09, 2015 at 09:05 UTC
      Anonymous Monk, You're not really answering my question other than telling me my thinking is wrong. I clearly asked what the best way to do bulk-pattern substitution is. If "smart match" is not the correct thinking, then please, feel free to elaborate on what the best way is... ;-)
        Are these the real regexes you want to "parallelize"?

        Step one (a): use one regex with a [char-class] with '\ and so on

        Step one (b): combine others with or-clauses in your regex (A|B)

        Step two: process more than one line at a time by joining them together.

        Cheers Rolf

        PS: Je suis Charlie!

        update

        added (one b)

        Anonymous Monk, You're not really answering my question other than telling me my thinking is wrong. I clearly asked what the best way to do bulk-pattern substitution is. If "smart match" is not the correct thinking, then please, feel free to elaborate on what the best way is... ;-)

        Lets, see , match operator matches, substitution operator substitutes ... understanding that one is apples and the other is oranges, even if they're both food, is very very important

        You've used a sufficient amount of "invented" vocabulary, so I gave you the answer in the form of a link

        but, you failed my litmus test, you didn't read the link I gave you, you didn't find the syntax you're looking for,

        so, we're at the point of waiting for you to read my link , and ask a question about it

Re: Smart match & substitution
by Anonymous Monk on Feb 09, 2015 at 11:52 UTC

    Smart matching, introduced in Perl v5.10, was retroactively made experimental, with warnings as of v5.18. That means the operator may undergo significant changes or even be removed later! In general, if you want your code to be compatible across many versions of Perl, using regular loops, map or grep instead of its distributive property is better.

    In this case, since in your example all the replacement parts of s/// are empty, the "looping over an array of regular expressions" technique is probably better. Also, unless the order of the substitutions you show in your example matters, maybe some of your regular expressions can be condensed (e.g. maybe s{[/?;.].*}{}?).

Re: Smart match & substitution
by CountZero (Bishop) on Feb 09, 2015 at 18:46 UTC
    An infinite number of regexes even when processed "faster than the speed of light" (whatever that may mean) will still take an infinite amount of time and no measure of optimizing it will make the slightest difference.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Well according to Albert would "faster than light" mean to travel back in time... (at least that's the interpretation of Minkowski diagrams) IIRC...

      Though you should go on diet first, since your mass would become infinite at speed of light, otherwise you'll have your personal episode of big bang theory. ;)

      Cheers Rolf

      PS: Je suis Charlie!

        Yes, indeed. An astute comment! Time will run backwards, but still for an infinite amount of (backwards) time.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Smart match & substitution
by Anonymous Monk on Feb 09, 2015 at 09:49 UTC
    Spaghetti code is when code jumps back and forth in a difficult to understand manner... what you've shown is not it. I see no particular problems with your code (other then it being kind of nonsense... maybe show something more real?)