$folder = '/tmp'; opendir TMPDIR, $folder or die "can't opendir $folder: $!\n"; while ( $filename = readdir TMPDIR ) { next unless $filename =~ /\.txt$/i; open FILE, "$filename" or die "can't open $filename: $!\n"; while() { if ($_ =~ /\s+(\S+)\s+(\S+)/) { $hash{$1} = $2; push(@index,$1); push (@a2, $2); } } close(FILE); # now do whatever } closedir TMPDIR;