in reply to Replace a hash key if it matches a regex
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;
...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replace a hash key if it matches a regex
by walkingthecow (Friar) on Sep 13, 2013 at 10:01 UTC | |
by choroba (Cardinal) on Sep 13, 2013 at 10:14 UTC | |
by walkingthecow (Friar) on Sep 14, 2013 at 03:56 UTC | |
by AnomalousMonk (Archbishop) on Sep 13, 2013 at 10:15 UTC |