CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
multiple string replace

 



orange
Novice

Mar 23, 2012, 5:17 AM

Post #1 of 3 (1430 views)
multiple string replace Can't Post

is there a way to simplify this search&replace:

$var1 =~ s//"/g;
$var1 =~ s//"/g;
$var2 =~ s//"/g;
$var2 =~ s//"/g;
$var3 =~ s//"/g;
$var3 =~ s//"/g;
$var4 =~ s//"/g;
$var4 =~ s//"/g;
$var5 =~ s//"/g;
$var5 =~ s//"/g;
$var6 =~ s//"/g;
$var6 =~ s//"/g;


naven8
Novice

Mar 23, 2012, 7:36 AM

Post #2 of 3 (1411 views)
Re: [orange] multiple string replace [In reply to] Can't Post

 
Try $var =~ s/" |' |" | :|/Output_string/gx;


BillKSmith
Veteran

Mar 23, 2012, 12:11 PM

Post #3 of 3 (1368 views)
Re: [orange] multiple string replace [In reply to] Can't Post

Your regular expression should consist only of a single character class containg only the two special characters. The rest of the substitution command is exactly as you codded it. However, each variable only needs to be processed once.



If you need to process all the variables with a single line in a loop, you need a 'symbolic reference' (refer: perldoc perlref) . This only works on global (not my) variables and is not allowed under "use strict".

You can avoid this problem by using a hash or an array rather than a list of similiar variable names.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives