<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 22/12/2010 10:44 AM, J. Bobby Lopez wrote:
<blockquote
cite="mid:AANLkTinJJxZzs=6fQkbDJtqaUyfcpjQseJ7PjM_45eB+@mail.gmail.com"
type="cite">Hey all,<br>
<br>
I'm trying to figure something out, but having some trouble. I'm
trying to replace multiple patterns via capture buffers, with
multiple replacements contained in a hash.<br>
<br>
Looking at the code below, you can see that the patterns I'd like
to match are contained in $regex just fine, however I'd like to
have mutiple $replacement{$#} 's depending on the number of
patterns I'm trying to replace.<br>
</blockquote>
I think you need the "e" flag if there is perl code in the
replacement part. Just stick "e" on the end of your substitutions.
You'll also need to use the . operator to concatenate as per:<br>
<br>
$line =~
s/$pattern/$replacement{$1}.$replacement{$2}.$replacement{$3}/e; #
kinda works now maybe<br>
<br>
All the best<br>
Stuart<br>
<br>
<blockquote
cite="mid:AANLkTinJJxZzs=6fQkbDJtqaUyfcpjQseJ7PjM_45eB+@mail.gmail.com"
type="cite">
<br>
==== begin-code ===<br>
#!/usr/bin/env perl<br>
use strict;<br>
use warnings;<br>
<br>
my %replacement;<br>
$replacement{'a'} = 'd';<br>
$replacement{'b'} = 'e';<br>
$replacement{'c'} = 'f';<br>
<br>
my $line = "*!*!* a *!*!* b *!*!* c *!*!*";<br>
<br>
#my $pattern = '(a)|(b)|(c)'; # Doesn't work, only matches first
pattern<br>
my $pattern = '(a).*(b).*(c)'; # kinda/sorta, replaces stuff
between patterns also<br>
<br>
#$line =~ s/$pattern/$replacement{$1}/; # <-- here's where I'm
having difficulty<br>
$line =~ s/$pattern/$replacement{$1} $replacement{$2}
$replacement{$3}/; # kinda/sorta<br>
<br>
#$line =~ m/$pattern/;<br>
#print "$1, $2, $3\n";<br>
<br>
print $line."\n";<br>
==== end-code ===<br>
<br>
<br>
Either I'm missing something, or this isn't actually possible, and
I'll have to loop through the patterns and replace them
individually.<br>
<br>
Thoughts?<br>
<br>
-Bobby<br>
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
toronto-pm mailing list
<a class="moz-txt-link-abbreviated" href="mailto:toronto-pm@pm.org">toronto-pm@pm.org</a>
<a class="moz-txt-link-freetext" href="http://mail.pm.org/mailman/listinfo/toronto-pm">http://mail.pm.org/mailman/listinfo/toronto-pm</a>
</pre>
</blockquote>
<br>
</body>
</html>