$count = 0; while(my $file = readdir(DIR)) { print "$file "; # can change \n to here. remember # to surround the while with and print "\n" unless (++$count % 5); }