Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Find pieces of text in a file enclosed by `@` and replace the inside

by bart (Canon)
on Mar 08, 2013 at 11:44 UTC ( #1022397=note: print w/replies, xml ) Need Help??

in reply to Find pieces of text in a file enclosed by `@` and replace the inside

Step one is that you replace text between '@' delimiters. You can do that using
s/\@(.*?)\@/ ... /g;
Step two is to replace individual characters on the selected part.

Using the /e modifier you can use perl code in the substitution part, where you can use $1 as a normal variable. With a pair of "{}" delimiters on the right hand side, it can even look like normal code, as it looks like a block; you have to use similar paired delimiters on the left to make it work, for example using angle brackets "<>":

s<\@(.*?)\@>{ ... }ge;
So you might try to do the replacement using code directly in the substitution part. But, to be safe, you'd better call a sub to do the actual replacement, on the selected text. I'd change your code like this:
while(<>) { s/\@(.*?)\@/ subst($1) /ge; print; } sub subst { my $s = shift; my %r = ( 'a' => chr(0x430), 'b' => chr(0x431), 'c' => chr(0x446), 'd' => chr(0x434), 'e' => chr(0x435), 'A' => chr(0x410), 'B' => chr(0x411), 'C' => chr(0x426) ); $s =~ s/([a-eA-C])/$r{$1}/g; return $s; }

Caveat: untested.

update: Tested, and bug fixed, this line was wrong:

s/\@(.*)\@/ subst($1) /ge;

Replies are listed 'Best First'.
Re^2: Find pieces of text in a file enclosed by `@` and replace the inside
by McA (Priest) on Mar 08, 2013 at 12:16 UTC


    one question to the (very nice) solution: I'm not sure whether I'm right. Is it possible that there are perl bugs in older versions with exactly this kind of substitution pattern:

    s/pattern/ func(bla) /ge;

    I'm just curious. Probably it was in another context. Where are the perl core hackers?

    Best regards

      I'm not sure what you mean. But I know that $1 and friends are block scoped, meaning that it's safe, and has always been safe, to nest substitutions provided the inner substitution was in a block.

      Calling a sub is one safe way to achieve that. It also has the added advantage that you don't have to take care of having to escape any unpaired nested braces: the substitution part may look like a code block to us, but it actually isn't.

Re^2: Find pieces of text in a file enclosed by `@` and replace the inside
by kluther (Novice) on Mar 08, 2013 at 11:50 UTC
    Thanks for your quick response. The code is not the solution. The second word of the first line is replaced also eventhough it is not surrounded by '@''s.
      Yeah, I just tested it. I forgot the question mark. Now it works.
        Wonderfull, thanks!
        Just one question. How do I exclude an email-address from processing?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022397]
and the monks are chillaxin'...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2017-07-24 23:27 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (361 votes). Check out past polls.