Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Combinations of lists, etc

by The Perlman (Scribe)
on Oct 04, 2019 at 16:23 UTC ( [id://11107055]=note: print w/replies, xml ) Need Help??


in reply to Re: Combinations of lists, etc
in thread Combinations of lists to a hash

> NB: You should take care to properly escape potential meta characters like * or ?

Is there a trivial way to do this?

- Ron

Replies are listed 'Best First'.
Re^3: Combinations of lists, etc
by AnomalousMonk (Archbishop) on Oct 04, 2019 at 17:20 UTC

    In general, meta-quoting for regexes (as used in split) can be done with quotemeta or with the  \Q ... \E intepolation escape sequences (see Quote and Quote-like Operators), which work for both double-quote and regex interpolation.


    Give a man a fish:  <%-{-{-{-<

      Are you sure this works with File::Glob 's meta characters sufficiently well?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        No. In fact, I hadn't even thought about the general case in which glob metacharacters may be included in the input string, i.e., the string to be permuted (if that's what you're referring to).

        Ferinstance, consider the input string 'Prefix{u,v}2={w,x}:b,{y,z}:1,2'. An attempt to fix this up so that glob produces

        Prefix{u,v}2={w,x}:b:1 Prefix{u,v}2={w,x}:{y,z}:1 Prefix{u,v}2={w,x}:b:2 Prefix{u,v}2={w,x}:{y,z}:2
        shows that there's a problem with the  , metacharacter: I can't figure out how to metaquote it.
        c:\@Work\Perl\monks\NorthernFox>perl -wMstrict -MData::Dump -le "my @proto_globules = ( 'Prefix{u,v}2={w,x}:b,{y,z}:1,2', ); ;; my $rx_comma_in_curlies = qr{ { .*? , .*? } }xms; ;; my $rx_globmeta = qr{ [\[\]{}*?~] }xms; ;; my %globize = ('=' => '={', ':' => '}:{', '' => '}'); ;; for my $globule (@proto_globules) { print qq{A: '$globule'}; $globule =~ s{ ($rx_comma_in_curlies) } { (my $r = $1) =~ s{,}{\\,}xmsg; $r; }xmsge; $globule =~ s{ (?= $rx_globmeta) }{\\}xmsg; print qq{B: '$globule'}; $globule =~ s{ ([=:] | \z) } { print qq{S: '$1'}; $globize{$1}; }xmsge; print qq{C: '$globule'}; my @globs = glob $globule; dd 'DD:', \@globs; print ''; next; } " A: 'Prefix{u,v}2={w,x}:b,{y,z}:1,2' B: 'Prefix\{u\,v\}2=\{w\,x\}:b,\{y\,z\}:1,2' S: '=' S: ':' S: ':' S: '' C: 'Prefix\{u\,v\}2={\{w\,x\}}:{b,\{y\,z\}}:{1,2}' ( "DD:", [ "Prefix{u\\,v}2={w\\:b:1", "Prefix{u\\,v}2={w\\:b:2", "Prefix{u\\,v}2={w\\:{y\\:1", "Prefix{u\\,v}2={w\\:{y\\:2", "Prefix{u\\,v}2={w\\:z}:1", "Prefix{u\\,v}2={w\\:z}:2", "Prefix{u\\,v}2=x}:b:1", "Prefix{u\\,v}2=x}:b:2", "Prefix{u\\,v}2=x}:{y\\:1", "Prefix{u\\,v}2=x}:{y\\:2", "Prefix{u\\,v}2=x}:z}:1", "Prefix{u\\,v}2=x}:z}:2", ], )
        The  \{u\,v\} isn't too bad, but wrapping a pair of  { } permuting curlies around  \{w\,x\} or  b,\{y\,z\} reveals that the comma is still "active":
        still active! -------+------------+ | | v v C: 'Prefix\{u\,v\}2={\{w\,x\}}:{b,\{y\,z\}}:{1,2}' ^ ^ ^ ^ | | | | in added curlies --+--------+-+----------+
        So unless someone can figure out how to metaquote the glob comma-in-curlies operator, it looks like a parser of some kind is the way to go in the general case.


        Give a man a fish:  <%-{-{-{-<

Re^3: Combinations of lists, etc
by LanX (Saint) on Oct 04, 2019 at 17:23 UTC
    Something like    s/[][\\?*{},]/\\$&/g might do. (Untested)

    But in the case that I'd worry about such input I'd rather write a loop multiplying arrays.

    That's easier to test. :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^3: Combinations of lists, etc
by 1nickt (Canon) on Oct 04, 2019 at 17:00 UTC
      Which tool exactly fulfills the OP's requirements? Could you demonstrate?
      - Ron

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-26 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found