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

"... 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

Replies are listed 'Best First'.
Re^4: In place replacement from reference list
by LanX (Saint) on Sep 09, 2022 at 01:27 UTC
    > ... 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