$sd = "../data/data_forthis"; opendir( DIR, $sd) || die; while( ($filename = readdir(DIR))){ next if "$sd\/$filename" =~ /\/\./; push @dots, "$sd\/$filename"; } ## end of while @dots = sort @dots; closedir(DIR); for(my $a=0;$a<@dots;$a++){ open (FILE, $dots[$a]); push @foo, ; if ($a+1 eq @dots) { close FILE; open (FILE, $dots[$a]); push @foo2, ; } ## end of if close FILE; } ## end of for ### At this point @foo has everything and $foo2 has just the last file in the directory...