use strict; use warnings; my @files = qw(001.file.a 001.file.b 002.file.a 002.file.b); my @sorted_files; for (@files){ if (m/^(\d+)\./){ push @{$sorted_files[$1]}, $_; } else { die "File name with unknown format: '$_'\n"; } } for my $bucket (0 .. $#sorted_files){ print "Processing bucket $bucket\n"; for (@{$sorted_files[$bucket]}){ print "\tprocessing file $_\n"; } } __END__ Processing bucket 0 Processing bucket 1 processing file 001.file.a processing file 001.file.b Processing bucket 2 processing file 002.file.a processing file 002.file.b