How can I use map () to apply an operation to each element of a hash in Perl?
I have some code that works great. It basically goes through each element of the hash with foreach()
and applies a transform to it using a regex like:
foreach my $key ( keys( %{$results} ) ) {
$results->{$key}{uri} =~ s/\".*\/(.*\.*(gif|jpe?g|png))\"/\/resources\/uploads\/$1/gi;
}
$ results is a hashref returnd with the DBI function fetchall_hashref()
.
Just out of curiosity, I wanted to see if I could rewrite it using map()
instead as shown below:
map {
$results{$_}{uri} =~ s/\".*\/(.*\.*(gif|jpe?g|png))\"/\/resources\/uploads\/$1/gi
} keys %{$results};
Unfortunately this doesn't work. I've tried all sorts of things but no success so far. Does anyone know how to do this? Thank.
UPDATE
Corrected code that ysth answers for :
map {
$results->{$_}{uri} =~ s/\".*\/(.*\.*(gif|jpe?g|png))\"/\/resources\/uploads\/$1/gi
} keys %{$results};
and an improved version with less prone toothpicks suggested by Sinan Ünür
map {
$results->{$_}{uri} =~ s{".*/(.*\.*(gif|jpe?g|png))"}{/resources/uploads/$1}gi
} keys %{$results};
source to share
The second part of the code uses a hash, but the first uses a hash reference. The following seems to work:
use warnings;
use strict;
my $results = {
a => {uri => "\"c/x.png\"" },
b => {uri => "\"d/y.jpeg\""}
};
map {
$results->{$_}{uri} =~
s/\".*\/(.*\.*(gif|jpe?g|png))\"/\/resources\/uploads\/$1/gi
} keys %{$results};
for my $k (keys %$results) {
print "$k $results->{$k}{uri}\n";
}