1 2 3 4
opendir my $dir, $path or die "Can not open $path for reading: $!\n"; @files1 = grep {/\.ex1$/i} readdir $dir; @files2 = grep {/\.ex2$/i} readdir $dir; closedir $dir;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#! /usr/bin/env perl use strict; use warnings; use 5.010; use Data::Dumper; my $path = '/path/to/dir'; my %files_by_ext; opendir my $dh, $path or die "opendir($path) failed: $!"; for my $entry ( readdir $dh ) { next if $entry =~ m/^\.\.?$/; my $ext = ( split m/\./, $entry )[-1]; next unless $ext; # push filename to HoA, key is filename's extension push @{ $files_by_ext{$ext} }, $entry; } closedir $dh; # check hash-of-arrays say Dumper \%files_by_ext; __END__
1 2 3 4 5
opendir my $dir, $path or die "Can not open $path for reading: $!\n"; @files = grep {/\.ex[12]$/i} readdir $dir; @files1 = grep {/1$/i} @files; @files2 = grep {/2$/i} @files; closedir $dir;