There are lots of ways of doing what you want. With Time::Piece which I believe is core as of Perl 5.9.5, you could do:
use Time::Piece;
my %hash = (
'key1' => '2018-01-17',
'key2' => '2018-02-05,2018-01-08',
'key3' => '2018-01-26' ,
'key4' => '2018-01-04',
'key5' => '2018-01-27,2018-02-06,2018-01-28'
);
my $current = localtime->strftime('%Y-%V');
foreach my $i (sort keys %hash) {
foreach my $j (split /\s*,\s*/, $hash{$i}) {
if( localtime->strptime($j, '%Y-%m-%d')->strftime('%Y-%V') eq $current ) {
print 'Key: ', $i, ', date: ', $j, "\n";
}
}
}
It uses the fact that %V
is the ISO 8601 week number, which concatenated with the year gives something to compare against.
The function strptime
turns strings into date-objects and then strftime
turns date-objects into strings.
The output:
Key: key2, date: 2018-02-05
Key: key5, date: 2018-02-06
The function strftime
has other week definitions which you may use: %U
, %V
and %W
; see for instance here.