Skip to content

Instantly share code, notes, and snippets.

@naquad
Created May 28, 2012 10:01
Show Gist options
  • Save naquad/2818284 to your computer and use it in GitHub Desktop.
Save naquad/2818284 to your computer and use it in GitHub Desktop.
sub filter(&@) {
local $_;
my ($code, $what) = @_;
my $t = ref($what);
if($t eq 'ARRAY'){
for(my $i = 0; $i < @$what; ++$i){
$_ = $what->[$i];
splice(@$what, $i--, 1) unless $code->($what->[$i]);
}
}elsif($t eq 'HASH'){
foreach my $k (keys %$what){
$_ = $what->{$k};
delete $what->{$k} unless $code->($k, $what->{$k});
}
}else{
die("Don't know how to filter $t!");
}
$what;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment