On Mon, 2008-09-22 at 10:19 -0700, Grant wrote: > I'd like to switch the " and ' characters in a block of text so that > all " characters become ' and all ' characters become ". The closest > thing I can come up with is the following, but of course it doesn't > work quite right because one is executed after the other: > > $code =~ s/"/'/g; > $code =~ s/'/"/g; > > Is there any way to switch them simultaneously? > > - Grant >
Yes. OK, here's how. #!/usr/bin/perl use strict; use warnings; my %Switch_Chars = ( '"' => "'", "'" => '"', ); while( <> ){ s/(["'])/$Switch_Chars{$1}/eg; print; } __END__ -- Just my 0.00000002 million dollars worth, Shawn Linux is obsolete. -- Andrew Tanenbaum -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/