use strict; use warnings; use Data::Dumper; my @txt_files = map {m!(.*\.txt)[^/]*$!} split /\n/, `ls -F`; # ^txt only ^want non-directories # and throw away the flag print Dumper(\@txt_files); my $num_files = $#txt_files + 1;