Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Good Idiom for Matching List?

by Abigail-II (Bishop)
on Dec 11, 2002 at 17:36 UTC ( [id://219136]=note: print w/replies, xml ) Need Help??


in reply to Good Idiom for Matching List?

Eh, why bother with the $goodnames = qr /$goodnames/ line? But, if you really wanted to, something like:
my $goodnames = do {local $" = "|"; qr {@{[map {"\Q$_"} @names]}}};

Abigail

Replies are listed 'Best First'.
Re: Re: Good Idiom for Matching List?
by TheDamian (Vicar) on Dec 11, 2002 at 20:38 UTC
    Abigail is (as usual) correct. However, if I were doing this, I'd code that as:
    my $goodnames = qr/@{[join '|', map "\Q$_", sort @names]}/;
    Or, if I were golfing:
    my $goodnames = qr/@{[join'|',map"\Q$_",sort@names]}/; ;-)

    And, if I were going to be interpolating arrays into regexes on a regular basis, I'd probably modularize the process thus:

    use Interpolation OR => sub { join '|', map "\Q$_", sort @{$_[0]} }; # and later... my $goodnames = qr/$OR{\@names}/;

    BTW, arrays will interpolate disjunctively in Perl 6, so eventually you'll be able to just write:

    # Perl 6 code my $goodnames = /@names/;

    Yet another feature for Abigail to not be impressed by, I guess. ;-)

      I think my problem with this construct, that is, why I think it should be natural and simple, is that what I'm writing is basically a composition of two functions.
      my $x= f(); $x= g($x);
      That is, the first result is the argument to the second line, and is not used for anything else.

      So I would naturally write it as my $x=g(f()); instead, as one expression without a named temporary.

      Here's the rub: qr// does not use the normal function syntax. It uses the quoting syntax, which doesn't naturally handle arbitrary nesting. So, use the @{[]} hack (or worse, if the list context would be a problem) or use Interpolation to work-around.

      But what I really want is a callable function that takes a string and returns the corresponding compiled regex. We have quotemeta and lc that let us access special syntax features in a normal functional way; I want that for everything.

      sub mk_qr ($) { eval "qr/$_[0]/"; }
      —John
      # Perl 6 code my $goodnames = /@names/;
      No qr? How can it tell that I didn't mean to match against $_ now and set $goodnames to the success condition?

      Update: Downvoters: This is a serious question, not a slam on Damian. I saw something "different", from someone who knows about it, and wanted more details. You can see from the reply that there is indeed something interesting and different happening here. In fact, I can imagine a section in the docs will discuss this very issue!

        In perl6, the /foo/ operator is qr-ish. However, in certian contexts (bool context, numeric context, stringy context, and basicly any other context that can't hold a regex), that means that the regex will be created, matched against, and then thrown away (that will probably get optimized a fair bit). However, yes, my $goodnames=/@names/ will set $goodnames to a regex that matches when any of the things in @names (taken literaly) match the target.


        Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Re: Good Idiom for Matching List?
by John M. Dlugosz (Monsignor) on Dec 11, 2002 at 17:47 UTC
    Why bother? Because it means I don't need grouping around it or the /o modifier. If I interpolate the (?: ... ) into $goodnames, I might as well turn it into a qr instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://219136]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found