in reply to Re: In place replacement from reference list
in thread In place replacement from reference list

> Perl's string handling functions are typically faster than regexes.

but in this case you can or all possible paths in a regex,

=~ m/^$path1|$path2|...etc/

and because of automatic Trie optimization this will be significantly faster than checking in a loop.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^3: In place replacement from reference list
by AnomalousMonk (Archbishop) on Sep 07, 2022 at 20:01 UTC
    =~ m/^$path1|$path2|...etc/

    I know LanX and kcott understand this, but here's a general side note. In a regex expression like the one quoted above, the ^ anchor is associated only with the first alternation, i.e., ^$path1. None of the other alternations are anchored.

    The "precedence" of Perl ordered alternation is very low. This applies generally, so in
        $str =~ m/ a b c | d | e | f g h /x;
    the regex pattern "atoms" a b c comprise the first possible alternation, then d if the first alternation cannot match, then e, then the f g h sequence.

    Use grouping, typically non-capturing, to disambiguate precedence. E.g., in
        $str =~ m/ a b (?: c | d e | ... | etc) f g /x;
    the sequence a b is required for a match, then the first of c or d e or ... or etc, then the required f g sequence.


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

      I didn't think about that, it wasn't meant to be productive code just an illustration.

      Thanks for pointing that out! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re^3: In place replacement from reference list
by kcott (Archbishop) on Sep 07, 2022 at 17:29 UTC
    "... in this case ... =~ m/^$path1|$path2|...etc/ ... significantly faster than checking in a loop."

    That's a use of alternation with which I'm unfamiliar.

    Please enlighten me as to how =~ m/^$path1|$path2|...etc/ could be used to generate groups of multiple QRY... lines, for the same input line, without a loop. E.g.

    QRY(4)="/a/bc.sh" QRY(4)="/ab/c.sh"

    — Ken

      > ... could be used to generate groups of multiple QRY... lines

      One way to do it is the (?{ collect() })(*FAIL) trick

      use v5.12; # https://perlmonks.org/?node_ +id=11146744 use warnings; use Data::Dump qw/pp dd/; #pp my ($paths,$cmds) = data(); my $re = join "|", map {"\Q$_\E" } @$paths; for my $cmd (@$cmds) { my @matches; $cmd =~ m{ ^CMD=" ($re) #/? # final / is missing (?!\.) # no empty name before .extens +ion ([^/]+) "$ (?{push @matches,[$1,$2]}) (*FAIL) }x; pp {$cmd => \@matches}; } sub data { return [ qw( /a /a/b /a/b/c /b /b/c /c /ab /abc /abcd )] , [ qw( CMD="/a/a.sh" CMD="/aa.sh" CMD="/ab.sh" CMD="/abc.sh" CMD="/a/bc.sh" CMD="/a/b/c.sh" CMD="/a/b/c/.sh" CMD="/a/b/cd.sh" CMD="/a/b/c/d.sh" CMD="/x/y.z" CMD="/a/xyz.sh" CMD="/abcd.sh" ), q(CMD="/a/very 'special' command.exe") ] }

      But while my results are in sync with

      >

      QRY(4)="/a/bc.sh" QRY(4)="/ab/c.sh"

      they differ significantly because my understanding is that the OP said that all CMDs have a missing final slash. I also disallowed files starting with a dot like .sh

      { "CMD=\"/a/a.sh\"" => [] } { "CMD=\"/aa.sh\"" => [["/a", "a.sh"]] } { "CMD=\"/ab.sh\"" => [["/a", "b.sh"]] } { "CMD=\"/abc.sh\"" => [["/a", "bc.sh"], ["/ab", "c.sh"]] } { "CMD=\"/a/bc.sh\"" => [["/a/b", "c.sh"]] } { "CMD=\"/a/b/c.sh\"" => [] } { "CMD=\"/a/b/c/.sh\"" => [] } { "CMD=\"/a/b/cd.sh\"" => [["/a/b/c", "d.sh"]] } { "CMD=\"/a/b/c/d.sh\"" => [] } { "CMD=\"/x/y.z\"" => [] } { "CMD=\"/a/xyz.sh\"" => [] } { "CMD=\"/abcd.sh\"" => [["/a", "bcd.sh"], ["/ab", "cd.sh"], ["/abc", +"d.sh"]], } { "CMD=\"/a/very 'special' command.exe\"" => [] }

      YMMV, but other interpretations of the OP are easily implemented by (un)commenting the two documented lines in the regex.

      update
      Output using
      /? # final / is missing #(?!\.) # no empty name before .exten +sion

      { "CMD=\"/a/a.sh\"" => [["/a", "a.sh"]] } { "CMD=\"/aa.sh\"" => [["/a", "a.sh"]] } { "CMD=\"/ab.sh\"" => [["/a", "b.sh"], ["/ab", ".sh"]] } { "CMD=\"/abc.sh\"" => [["/a", "bc.sh"], ["/ab", "c.sh"], ["/abc", ".s +h"]], } { "CMD=\"/a/bc.sh\"" => [["/a", "bc.sh"], ["/a/b", "c.sh"]] } { "CMD=\"/a/b/c.sh\"" => [["/a/b", "c.sh"], ["/a/b/c", ".sh"]] } { "CMD=\"/a/b/c/.sh\"" => [["/a/b/c", ".sh"]] } { "CMD=\"/a/b/cd.sh\"" => [["/a/b", "cd.sh"], ["/a/b/c", "d.sh"]] } { "CMD=\"/a/b/c/d.sh\"" => [["/a/b/c", "d.sh"]] } { "CMD=\"/x/y.z\"" => [] } { "CMD=\"/a/xyz.sh\"" => [["/a", "xyz.sh"]] } { "CMD=\"/abcd.sh\"" => [ ["/a", "bcd.sh"], ["/ab", "cd.sh"], ["/abc", "d.sh"], ["/abcd", ".sh"], ], } { "CMD=\"/a/very 'special' command.exe\"" => [["/a", "very 'special' c +ommand.exe"]], }

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery