Hi monks,

This seems like it should be straightforward, but I'm clearly missing something.I'm trying to iterate over lists of substituion REs, and the substitutions aren't substituting.

I have something like (heavily simplified):

%regexes = (first => ['(\w+) (\w+) (\w+)', '$1 $3'], second => ['and so on', 'and on'] ); my $string = "big hairy mess"; foreach my $re (keys %regexes) { $string =~ s/$regexes{$re}[0]/$regexes{$re}[1]/i; print "$re: $string\n"; }
The 'first' regex, instead of printing 'first: big mess' prints 'first: $1 $3'.

I've tried adding the /e switch, which didn't help, and wrapping the backreferences in (?{}) blocks, which didn't help.

I'm sure I'm missing something stupid, but can't figure out what that might be. Hints?


In reply to Storing substitution patterns by abulafia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.