foreach (keys $db_results) { my $match = $1 if $_ =~ /(BLAH\d{2}-\w)/; $db_results->{$1} = delete $db_results->{$_}; }
The $match variable isn't used in the quoted code, so I assume this is a cut-down fragment of working code in which it is subsequently used. If so, be aware that the usage
my $scalar = $whatever if ... ;
(conditional definition/initialization of a lexical) is a pre-state (5.10+) hack used to create a bastard state-like variable; it has all kinda weird side-effects and is officially Frowned Upon. Avoid it if you can — and you can, e.g.:
my ($match) = $_ =~ /(whatever)/;
do_something_with($match) if defined $match;
or (assuming you're in a loop)
my ($match) = $_ =~ /(whatever)/;
next unless defined $match;
...
In reply to Re: Replace a hash key if it matches a regex
by AnomalousMonk
in thread Replace a hash key if it matches a regex
by walkingthecow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |