in reply to Using a regex function from a hash
Another thing you could do, if you only want to apply regex substitutions, is to make the values of the hash list references, where the first item in the list is the left side of the substitution, and the second item is the right side of the substitution.
Here's an example:
#!/usr/bin/perl -w use strict; use warnings; + my @views = ( "ignore", "abbbbc", "mn123op", "ignore", "uvwxy", ); my %functions_hash = ( 'ab+c$' => [ 'b+', 'x' ], 'mn\d+' => [ '(\d+)', '<number>' ], 'uvwxy' => [ 'uvw', 'UVW' ], ); foreach my $data (@views) { while (my ($key, $pvalue) = each %functions_hash) { if ($data =~ /$key/) { my ($from, $to) = ($pvalue->[0], $pvalue->[1]); $data =~ s/$from/$to/; print "data is now $data\n"; } } }
Which gives these results:
data is now axc data is now mn<number>op data is now UVWxy
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using a regex function from a hash
by monarch (Priest) on Aug 03, 2006 at 08:56 UTC |