Reputation: 25
I have the following hash:
%{$data{$id}{$date}}
that includes a large number of dates
and an array @dates
that includes a subsection of dates found in the %hash
.
What is the best way to loop through my %hash
and delete all dates that are not found in @dates
array? Once this is done I'd like the %hash
to only have the values from @array
dates.
I've tried 'delete unless exists' looking at keys after I created a hash from my @dates
array but would get missing arguments error.
Upvotes: 2
Views: 223
Reputation: 107115
You should loop through the keys of %{$data{$id}}
instead.
my %dates = map {$_ => 1} @dates;
exists $dates{$_} or delete $data{$id}{$_} for keys %{$data{$id}};
Upvotes: 4
Reputation: 126762
As long as the dates in @dates
are guaranteed to appear in the hash you can use map
%{ $data{$id} } = map { $_ => $data{$id}{$_} } @dates
Upvotes: 3