opendir(DIR, $top.$second) or die "CANNOT OPEN SECOND DIRECTORY\n"; @nameFolders = grep { !/^\.|\.\.$/ } readdir(DIR); closedir(DIR); foreach(@nameFolders){ $folder = $_; if($_ =~ /\.txt$/){ next; } #sometimes .txt files are here but I took care of them earlier in the code and that works just fine #print $_."\n"; #Output is 100% perfect here opendir FIL, $top.$second."/".$folder or die "CANNOT OPEN NAME DIRECTORY\n"; @files = grep { /\.txt$/ } readdir(FIL); closedir(FIL); foreach(@files){ $fileName = $_; print $fileName."\n"; #HERE IS MY PROBLEM OUTPUT IS BELOW @fileName = split / /, $fileName; $numID = $fileName[0]; $goodFiles{$fileName}=$numID; } } #### OUTPUT: 1234 ACS. (STUFF).txt ACS. 1235 ACS. (STUFF).txt ACS. ...