in reply to string substitution

use v5.12; use warnings; # https://perlmonks.org/?node_id=11141581 my $in = "anXwxdXdzd"; my $pi = "1415926535"; my @in = split //, $in; my @pi = split //, $pi; my $re = join "", map {$in[$_] eq "X" ? $pi[$_] : '.' } 0..9; $re = "/bin/grep $re /path/to/some/file"; say $re;
OUTPUT:
/bin/grep ..1...6... /path/to/some/file

edit

FWIW: Perl can apply mask operations with & , ^ and | , but this would also first require translating X to chr(255) and else to chr(0). see tr and s for that approach.

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

updates

Replies are listed 'Best First'.
Re^2: string substitution
by LanX (Saint) on Feb 23, 2022 at 12:41 UTC
    > FWIW: Perl can apply mask operations with & , ^ and | , but this would also first require translating X to chr(255) and else to chr(0). see tr and s for that approach.

    (used uppercase X for demo only, switching to lowercase works too)

    use v5.12; use warnings; # https://perlmonks.org/?node_id=11141581 my $in = "anXwxdXdzd"; my $pi = "1415926535"; my $mask = $in =~ tr/Xa-z/\xff\x00/r; my $re = $pi & $mask; $re=~ tr/\x00/./; $re = "/bin/grep $re /path/to/some/file"; say $re;
    OUTPUT:
    /bin/grep ..1...6... /path/to/some/file

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

Re^2: string substitution
by Anonymous Monk on Feb 23, 2022 at 12:44 UTC
    That worked perfectly. Thank you. I have a twist to add, though. After creating the initial regexp, I want to now check for the letter 'q' in the input string. For each 'q' found, I want to append (for example, with a 'q' in the fourth character of the input line) to the regexp:

    ' | grep -v ...5......'
    So now, instead of a positive match, I want to check for a negative match (and if I understand regexes, I would need to add that string for each 'q' found in the input line).
      You should at least provide valid data in the sense of an SSCCE, your description is cryptic.

      Anyway, ISTM you want a character class [^5] in the regex to match anything but 5.

      Please expand the code provided to your needs, and show us what you tried.

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