my $path = '/path/to/dir'; my @names = glob $path . '/graph_set_*.out'; # @names is in alpha order, so sort numerically my @sorted_names = sort { my ($x, $y); $a =~ /graph_set_(\d+)\.out$/ and $x = $1; $b =~ /graph_set_(\d+)\.out$/ and $y = $1; $x <=> $y } @names; # That may benefit from a Schwartzian Transform # Now, do the loop for (@sorted_names) { open local(*IN), "< $_" or die $!; # ... close IN or die $!; }