# This is untested code! my @dlcopy = @dir_list ; my @extensions = qw(zip log whatever) ; my %matches ; foreach my $ext (@extensions) { my @nonmathing ; my @matching ; foreach my $file (@dlcopy) { if ($file =~ /\.$ext$/) { push @matching,$file ; } else { push @nonmatching,$file ; } } @dlcopy = @nonmatching ; $matches{$ext} = \@matching ; }