Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Good Idiom for Matching List?

by TheDamian (Priest)
on Dec 11, 2002 at 20:38 UTC ( #219168=note: print w/ replies, xml ) Need Help??


in reply to Re: Good Idiom for Matching List?
in thread Good Idiom for Matching List?

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. ;-)


Comment on Re: Re: Good Idiom for Matching List?
Select or Download Code
Re: Re: Re: Good Idiom for Matching List?
by John M. Dlugosz (Monsignor) on Dec 11, 2002 at 21:05 UTC
    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
typo or more changes coming our way?
by John M. Dlugosz (Monsignor) on Dec 11, 2002 at 21:13 UTC
    # 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).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-07-25 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls