in reply to substitution with regex

Hi drose2211,

Other monks have given good solutions, but you might also be want to know what your mistakes are.

First, you can't use the $1 and $2 special variables in a formula before they have been set by a regular expression. In other word, they are undefined at the point where you're using them in the conversion formula.

Second, I am not sure why you enclose them within curly braces then is no need to do that. If you want to isolate them from neighboring characters (not needed here), then the proper syntax is ${1}, not {$1}.

Finally, it seems from the error messages you get that your input file is not properly opened. When you open a file, you should always check if the statement worked:

open IN,'<',"part2.txt" or die "could not open file part2.txt $!";
Update: fixed a typo on the filename on the preceding line. Thanks, soonix.

Replies are listed 'Best First'.
Re^2: substitution with regex
by soonix (Chancellor) on Jan 19, 2018 at 12:46 UTC
    open IN,'<',"part2.txt" or die "could not open file part2;txt $!";
    Yep, and that's one reason why I prefer putting filenames in variables :-) (otherwise, ++, of course)
      I prefer putting filenames in variables
      Yes soonix++, and that's what I also do systematically in actual code. When suggesting corrections on a forum, however, I tend to stress only the most essential suggestions, to avoid distracting attention with more secondary matters.