in reply to Evaluating a regex replacement value

Just want to point out that you can also using symbolic substitution, which was how I interpreted the OP's "substituting certain keywords with variables".   Although I made a recent argument in favor of always being in the habit of using strict, I can see why this might be a good situation for using "no strict 'refs'":
#!/usr/bin/perl -w + use warnings; use strict; no strict 'refs'; + our $a = 10; our $b = 20; our $c = 33; our $d = 123; + my @lines = qw( <:a:0:> <:a:3:> <:b:0:> <:b:4:> <:c:5:> <:d:6:> ); + foreach my $line (@lines) { if ($line =~ s/<:(\w+):(\d+):>/$$1*($2||1)/e) { printf "Result = %d\n", $line; } }
Output is:
    Result = 10
    Result = 30
    Result = 20
    Result = 80
    Result = 165
    Result = 738

Replies are listed 'Best First'.
Re^2: Evaluating a regex replacement value
by McDarren (Abbot) on Oct 05, 2005 at 14:49 UTC
    heh... "substituting certain keywords with variables"... was badly worded - what I should have said was: "substituting certain keywords with the values of associated variables"

    For example, I have stuff like:
    $line =~ s/<:INCOMING:([^:>]*):?>/int($incoming * ($1 || 1))/e; $line =~ s/<:OUTGOING:([^:>]*):?>/int($outgoing * ($1 || 1))/e; $line =~ s/<:INGRESS:>/$eth1/; $line =~ s/<:EGRESS:>/$eth0/; if ($line =~ /<:RADIUS:>/) { foreach my $ip (@radius_ips) { $thisline = $line; $thisline =~ s/<:RADIUS:>/$ip/; print OUT $thisline; } }
    (I'm in the process of abstracting the keyword/value pairs into a lookup hash, as per my previous post)