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

hallo,

I've follwing code that works well. It replaces all occurences of mytag in @TAGS with my_new_tag:
my %translation = ( "(mytag)(.*?)" => "my_new_tag", "(mytag2)(.*?)" => "my_new_tag2" ); foreach $tag(@TAGS){ foreach $key (keys %translation){ if($tag =~ /$key/gs){ $do_something_with($transaltion{$key}, $1, $2); } } }
now I try to use $1 and $2 directly in the hash, but it does not work!?
my %translation = ( "(meintag)(.*)" => { "tag" => $1."new", "value" => $2} ); foreach $tag (@TAGS){ foreach $key (keys %translation){ if($tag =~ /$key/gs){ $do_something_with($translation{$key}->{tag}, $translation{$key}->{val +ue}); } } }
Does anybody have a solution / workaround for this?
In the final version I want to load the translation hash via a ini file.


Thank you in advance

Michael

Replies are listed 'Best First'.
Re: using $1 in a hash?
by VSarkiss (Monsignor) on Feb 05, 2004 at 18:16 UTC

    In your second example, $1 and $2 are not set at the time you're creating the %translation hash. Those are only set after a successful capturing match.

    Rather than help you with the regex, I think I'd rather pont you at Config::Inifiles, which may do most of the task for you.