Xiao Lan (小兰) wrote:
Hello,

Hello,

I have a text whose content is like:

aabbbbaa
bbaaaabbaa
aaxxxbb
bb776yhghhaa


I want to switch all "aa" and "bb".
 I can handle this use regex:

s/aa|bb/$& eq 'aa'?'bb':'aa'/ge;


But how to use 'tr' doing that?

$ perl -e'
my @data = qw/
    aabbbbaa
    bbaaaabbaa
    aaxxxbb
    bb776yhghhaa
    /;
for ( @data ) {
    print "$_ to ";
    s/(aa|bb)/ ( my $x = $1 ) =~ tr|ab|ba|; $x /ge;
    print "$_\n";
    }
'
aabbbbaa to bbaaaabb
bbaaaabbaa to aabbbbaabb
aaxxxbb to bbxxxaa
bb776yhghhaa to aa776yhghhbb




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to