verbatim
verbatim

Reputation: 229

How to properly chain Perl substitutions

I notice that when directly in terminal it is easy to chain substitutions together:

perl -p -i -e 's/\xa/\xd/g; s/(<ACROSS PUZZLE>.+<GRID>\r\t)([A-Z\.]{15,})(\r\t.+)/\2/g' _temp.txt;

However, I am not having success combining two substitutions together as I do a rewrite of some scripting I have. Currently, as two separate statements, it works, but I am not sure how to combine these two lines together:

$thisGrid =~ s/\xa/\xd/g;
$thisGrid =~ s/(<ACROSS PUZZLE>.+<GRID>\r\t)([A-Z\.]{15,})(\r\t.+)/\2/g; 

First I tried:

$thisGrid =~ s/\xa/\xd/g =~ s/(<ACROSS PUZZLE>.+<GRID>\r\t)([A-Z\.]{15,})(\r\t.+)/\2/g; 

This did not seem to work.

Other similar threads did not seem to answer my question.

Upvotes: 2

Views: 497

Answers (1)

ikegami
ikegami

Reputation: 386706

A foreach loop can be used as a topicalizer.

for ($thisGrid) {
   s/\x0A/\x0D/g;
   s/(?:<ACROSS PUZZLE>.+<GRID>\r\t)([A-Z\.]{15,})(?:\r\t.+)/$1/g; 
}

Otherwise, you need to use /r to return the modified string.

$thisGrid =
   $thisGrid
   =~ s/\x0A/\x0D/gr
   =~ s/(?:<ACROSS PUZZLE>.+<GRID>\r\t)([A-Z\.]{15,})(?:\r\t.+)/$1/gr;

You can chain them

Upvotes: 7

Related Questions