Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic regexp

3 views
Skip to first unread message

Winston Smith

unread,
Nov 13, 2003, 11:54:23 PM11/13/03
to
Hi everybody,

I'm looking for a way to make a batch of s/// substitutions. As a code
sample is worth a thousand words, let see what me code is presently :

---

my @rules = (
['^HELLO (.*)$', 'BONJOUR $1'],
# ... lots of other rules
);

foreach my $rule (@rules) {
if ($string =~ s/$rule->[0]/$rule->[1]/ei) {
last;
}
}

---

With this code, 'HELLO WINSTON' becomes 'BONJOUR $1' and not 'BONJOUR
WINSTON' as I'd like.

I tried to put double quotes in the table of strings but it makes no
difference.

I also tried to put a second e option to the s/// operator so that the
string 'BONJOUR $1' is reinterpolated. Then I get the message 'Use of
uninitialized value in substitution iterator ...' as if $1 is not
defined. But if I put a
print $1;
instruction just before the instruction
last;
it works and actually print 'WINSTON' if I use the same exemple than
previously.

Thank you in advance for your help.

Klaus Johannes Rusch

unread,
Nov 14, 2003, 11:12:39 AM11/14/03
to
Winston Smith wrote:

> I'm looking for a way to make a batch of s/// substitutions. As a code
> sample is worth a thousand words, let see what me code is presently :
>
> ---
>
> my @rules = (
> ['^HELLO (.*)$', 'BONJOUR $1'],
> # ... lots of other rules
> );
>
> foreach my $rule (@rules) {
> if ($string =~ s/$rule->[0]/$rule->[1]/ei) {
> last;
> }
> }
>
> ---
>
> With this code, 'HELLO WINSTON' becomes 'BONJOUR $1' and not 'BONJOUR
> WINSTON' as I'd like.

my @rules = (
['^HELLO (.*)$', '"BONJOUR $1"'],
);


foreach my $rule (@rules) {
if ($string =~ s/$rule->[0]/eval($rule->[1])/ei) {
last;
}
}


--
Klaus Johannes Rusch
Klaus...@atmedia.net
http://www.atmedia.net/KlausRusch/

0 new messages