pysome has asked for the wisdom of the Perl Monks concerning the following question:

How to eval an array element in regex's substitution ?
Dear All,
I wanna get "Hello zy" in following code. I try to add /e and /ee, all they can't work.
my $s = 'Hello [2][1]'; my @a =('x','y','z'); my $replace ='$a'; $s =~ s/(\[\d{1,2}\])/$replace\1/g; ## only output : Hello $ +a[2]$a[1] print $s;
Pls give me a hand. THX.



Replies are listed 'Best First'.
Re: How to eval an array element in regex's substitution
by ikegami (Patriarch) on Oct 27, 2010 at 03:36 UTC

    First, \1, etc are regex patterns. You want $1, etc.

    Secondly, you have some serious misconception about the replacement expression.
    Without /e, it's a double-quoted string literal that evaluates to the replacement.
    With /e, it's a Perl expression which evaluates to the replacement.

    my $s = 'Hello [2][1]'; my @a = ('x','y','z'); $s =~ s/\[(\d{1,2})\]/$a[$1]/g; print $s;
      Thank your very much.
      Another question, maybe i refer to capture the square brackets ,
      and then "eval" the array element. Such as:
      $s =~ s/(\[\d{1,2}\])/$a$1/g; #output Hello $a[2]$a[1] # eval '$s="$s";'; ## Here ?
      How to eval the $s's value?
        my $s = 'Hello [2][1]'; my @a = ('x','y','z'); my $array_name = 'a'; { no strict 'refs'; $s =~ s/\[(\d{1,2})\]/$array_name->[$1]/g; } print $s;


        my $s = 'Hello [2][1]'; my @a = ('x','y','z'); my $array_name = 'a'; my $array_ref = do { no strict 'refs'; \@$array_name }; $s =~ s/\[(\d{1,2})\]/$array_ref->[$1]/g; print $s;

        But why do you want to use variable variable names?


        my $s = 'Hello [2][1]'; my @a = ('x','y','z'); my $array_ref = \@a; $s =~ s/\[(\d{1,2})\]/$array_ref->[$1]/g; print $s;


        my $s = 'Hello [2][1]'; my @a = ('x','y','z'); $s =~ s/\[(\d{1,2})\]/$a[$1]/g; print $s;
Re: How to eval an array element in regex's substitution
by aquarium (Curate) on Oct 27, 2010 at 04:23 UTC
    In the scope/context provided, it doesn't make sense to use regex to achieve "Hello zy". If it's always going to be "Hello " followed by index 2 and index 1 of @a array, then just make it: my $s = 'Hello ' . $a[2] . $a[1]; which can be easily parametarised in that format. Or even use splice. why the regex?
    the hardest line to type correctly is: stty erase ^H
      oh i get it, i think...this is the continuing saga of a mail merge. in my own opinion, you need to learn a bit more about *nix utilities, networking protocols (smtp for a start), and programming (the design aspect). otherwise you'll end up throwing together "solutions" that hang by a thread. it's not a criticism of you or your knowledge, but you seem to have currently a level of knowledge where you could make something nice or a mess. you need (i think) a more rigorous structure/modularity in your code and overall approach. which means more thinking and less coding. like everything else here this is just an opinion and you can just ignore it.
      the hardest line to type correctly is: stty erase ^H
        Maybe you think it simply.
        The $s is dynamic.It is maybe  "[2][1]" or maybe "[9]  [8] [3]",
        . I only wanna to replace the  [xx]with a fix array's element.
        But the rule is , the  [xx] is index of the array!