#!/usr/bin/perl -w use strict; my @array = qw(001.file.a 001.file.b 002.file.a 002.file.b); my %hash; for my $filename (@array){ $filename =~ /^(\d+)/; push @{$hash{$1}}, $filename; } for my $group (sort { $a <=> $b } keys %hash){ print "Processing group '$group':\n"; for my $file (@{$hash{$group}}){ print "\tProcessing $file:\n"; ### Do stuff } }