Loop through the array, and delete.
use Data::Dumper; use strict; use warnings; my %hash = (a => 3, b=> 4, c=>1 ,d=>7); my @sels = qw(b c); delete $hash{$_} foreach (@sels); print Dumper \%hash;
Update: Enlil's way is obviously faster and more straight.
use Data::Dumper; use Benchmark qw(timethese); use strict; use warnings; timethese(1000000, { 'loop' => \&s1, 'non-loop' => \&s2 }); sub s1 { my %hash = (a => 3, b=> 4, c=>1 ,d=>7); my @sels = qw(b c); delete $hash{$_} foreach (@sels); } sub s2 { my %hash = (a => 3, b=> 4, c=>1 ,d=>7); my @sels = qw(b c); delete @hash{@sels}; }
Result:
Benchmark: timing 1000000 iterations of loop, non-loop... loop: 10 wallclock secs ( 9.84 usr + 0.03 sys = 9.87 CPU) @ 10 +1276.08/s (n=1000000) non-loop: 8 wallclock secs ( 6.88 usr + 0.02 sys = 6.89 CPU) @ 14 +5116.82/s (n=1000000)
In reply to Re: Slicing and Destructing a Hash
by pg
in thread Slicing and Destructing a Hash
by neversaint
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |