wa2nlinux has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I trying to make code to find and replace a file text, using a list of 'find/replace' pattern and writting to a file here the code
#!/usr/bin/perl use strict; use warnings; my @replace = ( [ qr/\/ka/ => '"ku"' ], [ qr/\/hi/ => '"he he he"' ], ); my $s = "/ka /hi"; for my $replace (@replace) { $s =~ s/$replace->[0]/$replace->[1]/gee; } open(FileHTM,">newfile") || die("Cannot Open File"); print FileHTM "$s\n"; close(FileHTM)
The question is how can I put
my @replace = ( [ qr/\/ka/ => '"ku"' ], [ qr/\/hi/ => '"he he he"' ], );
into a separate file because I need to write many more list on that. Also my @s (source text) should be a file too.

Replies are listed 'Best First'.
Re: Find Replace text with list
by moritz (Cardinal) on Feb 07, 2011 at 07:55 UTC

    I see that you are already using file operations in your script. What stops you from simply reading the contents of a file into $s?

    For the @replace array you can just read a file line by line and split on a predefined delimiter.

      Here the error when I try to get @replace from file
      Can't use string ("my @replace = ( ") as an ARRAY ref while "strict refs" in use at parsingtex.pl line 16 +, <FData> line 4.
      I saw you can do a multiple find and replace a list of items in text online at http://list-replace.info . Maybe this can help.
Re: Find Replace text with list
by roboticus (Chancellor) on Feb 07, 2011 at 13:10 UTC

    wa2nlinux:

    I assume the part you're having trouble with is getting the regular expression into a variable and treating it as a regular expression instead of a string. If you read perldoc perlop in the section on "Quote and quote-like operators", you'll find the subsection "Regex Quote-Like Operators" which will help you.

    Basically the trick is this: Once you get the regex into a string, you can use the qr operator to turn it into a reference to a regex that you can use just like a regular expression:

    $ cat regex_in_str.pl #!/usr/bin/perl use strict; use warnings; my $r = "ab+c*d"; my $rRegex = qr/$r/; for my $t (qw( abcd aabbbd acccd dbca )) { if ($t =~ $rRegex) { print "MATCH: $t\n"; } } $ perl regex_in_str.pl MATCH: abcd MATCH: aabbbd $

    So all you need to do is read the name and representation of your regular expression from a file, convert the representation into a reference to a regex and load your array.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      How could I qw each line of file ?
      @lines=<FILE>; my $t = qw (@lines)
      not working ?

        wa2nlinux:

        You don't need to. I used qw just for making the example. Perhaps I should've written it like this:

        #!/usr/bin/perl use strict; use warnings; my $r = "ab+c*d"; my $rRegex = qr/$r/; while (my $t = <DATA>) { if ($t =~ $rRegex) { print "MATCH: $t\n"; } } __DATA__ abcd aabbbd acccd dbca

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.