I have written a Perl script which opens a directory consisting of various files. It seems that the script does not read the files in any sequential order (neither alphabetically nor size wise) instead it reads them randomly. I was wondering what could be the reason behind the same?
I have written a Perl script which opens a directory consisting of various files.
Share
It’s never random, it’s just in a pattern that you don’t recognize. If you look at the documentation that describes the implementation of whatever function you’re using to read the directory, it would probably say something like, does not guarantee the order of the files to be read.
If you need them in a specific order, sort the names before you operate on them.
The files are probably read in an order that’s convenient for the underlying file system. So, in a sense, the files are ordered, but not in an order you expect (size or alphabetical). Sometimes, files have an internal numerical id, and the files may be returned in numerical order given this id. But this id is something you probably won’t encounter often if ever.
Again, the results are ordered, not random. They’re just in an order that you’re not expecting. If you need them ordered, order them explicitly.
See also: http://www.perlmonks.org/?node_id=175864