in reply to Using a regex function from a hash
and the result is:use strict; my @views = ("ignore","ignore","dont\\ignore\\this.vms","ignore"); my %regex_hash = ( '\.vms' => [ qr/\\/ , '/' ], ); for my $data (@views) { for my $re_key ( grep { $data =~ /$_/ } keys %regex_hash ) { $data =~ s!$regex_hash{$re_key}[0]!$regex_hash{$re_key}[1]!g; } } print join "\n", @views;
In regex_hash the key is the regex (as a string) to match against the view data. The first entry in the value arrayref is the match portion of the substitution. The second entry in the value arrayref is the replace portion of the substitution.ignore ignore dont/ignore/this.vms
You'd have to be careful of using ! (in this implementation) in the key or the second value because I used it for the substitution delimiter.
Becuase of this and the flexibility offered by a sub dispatch table you're probably better off going that way, but I thought I'd show you how close you were to making it work!
Update: Just realised that liverpole already pointed this out. Using qr as in my example is a good idea however - it is more efficient and will protect you against quoting issues. See perlre if you are unfamiliar with qr
|
---|