my @dlcopy = @dir_list ; my @extensions = qw(zip log whatever) ; my %matches ; foreach my $ext (@extensions) { my $nonmatching = [] ; my $matching = [] ; my $pattern = qr(\.$ext$) ; foreach my $file (@dlcopy) { push @{$file =~ /$pattern/ ? $matching : $nonmatching},$file ; } @dlcopy = @$nonmatching ; $matches{$ext} = $matching ; }