in reply to adding to an array from foreach loop

As a side note:
# ... else { # print "not enought files to count\n"; } } } print "total files in $total\n"; };
Gee, this really looks bad.

Formatting and especially indenting your code correctly is not optional, it is a necessity. The compiler might not care, but the human reader does.