Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

interpolation in replacement part

by anniyan (Monk)
on Oct 14, 2005 at 15:49 UTC ( #500271=perlquestion: print w/ replies, xml ) Need Help??
anniyan has asked for the wisdom of the Perl Monks concerning the following question:

hello monks, i have come up with a problem where i could not able to search anywhere.

i have hash in which key and value are patterns.

key:<date>([A-Z&;][a-z&;]+), ([0-9]{2}) ([0-9]{4})</date> value:<month>$1</month>

when i used this above hash in the regular expression it matches in the matching but in the replacement part, the interpolation is not taking place.

So i need your help, i also tried using 'e' option modifiers twice to evaluate twice, but didnt got.

I tried with following code,

$instr = '<date>Jan, 10 1997</date>'; for my $pat (keys %patt1) { print "$pat\t$patt1{$pat}\n"; $instr =~ s|$pat|$patt1{$pat}|egs;#this is not working $instr =~ s|$pat|<month>$1</month>|gs;#this works }

For the string, <date>Jan, 10 1997</date>, when i used $1 separately in replacement part it works fine, where i get <month>Jan</month>, but when i used hash it is not getting interpolated.

Please find where i am going wrong.

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Comment on interpolation in replacement part
Select or Download Code
Re: interpolation in replacement part
by tirwhan (Abbot) on Oct 14, 2005 at 16:02 UTC

    Put brackets around the hash element to explicitly interpolate:

    $instr =~ s|$pat|${patt1{$pat}}|egs;

    Update: sorry, brainfreeze, this will not work

    2nd Update:This node is getting upvoted even though I marked it as wrong, so I thought I'd clarify:This advice is wrong and won't work, please don't use it, it was conceived and written in an inexplicable moment of fuzzy-headedness. runrig's solution below works perfectly.

      Sorry to say, it is not working.

      Regards,
      Anniyan
      (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Re: interpolation in replacement part
by runrig (Abbot) on Oct 14, 2005 at 16:17 UTC
    Did you escape the $1 in the value? Also, you will need to double evaluate, and fix what you are evaluating:
    my %patt1; my $key = "<date>([A-Z&;][a-z&;]+), ([0-9]{2}) ([0-9]{4})</date>"; my $value = "qq|<month>\$1</month>|"; $patt1{$key} = $value; $instr = '<date>Jan, 10 1997</date>'; for my $pat (keys %patt1) { if ( $instr =~ s|$pat|$patt1{$pat}|eegs ) { print "match1: $instr\n"; } }
    If you're doing this repeatedly in your program, though, it would be better to compile the regexes with qr and store them in an array rather than as keys in a hash.

      I was struggling with sprintfs inside the substitution. Didn't know that double quoting trick. ++.

      --
      David Serrano

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://500271]
Approved by Samy_rio
Front-paged by Roy Johnson
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2015-07-04 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls