为什么 Perl 脚本以随机顺序从目录中读取文件?

发布于 2024-07-23 10:56:01 字数 95 浏览 3 评论 0原文

我编写了一个 Perl 脚本,它打开一个包含各种文件的目录。 脚本似乎不按任何顺序读取文件(既不按字母顺序也不按大小顺序),而是随机读取它们。 我想知道这背后的原因是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晒暮凉 2024-07-30 10:56:01

它从来都不是随机的,它只是以一种你无法识别的模式出现。 如果您查看描述用于读取目录的任何函数的实现的文档,它可能会说类似“不保证读取文件的顺序”之类的内容。

如果您需要按特定顺序排列它们,请在对其进行操作之前对名称进行排序。

这些文件可能按照对底层文件系统来说方便的顺序读取。 因此,从某种意义上说,文件是有序的,但不是按照您期望的顺序(大小或字母顺序)。 有时,文件有一个内部数字 ID,并且文件可能会按照给定此 ID 的数字顺序返回。 但这个 id 是你可能不会经常遇到的东西。

同样,结果是有序的,而不是随机的。 它们只是按照您意想不到的顺序排列。 如果您需要订购它们,请明确订购。

另请参阅:http://www.perlmonks.org/?node_id=175864

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

命硬 2024-07-30 10:56:01

它可能会根据它们在目录文件列表中存储的顺序来读取它们。 在某些类 Unix 文件系统上,目录本质上是指向内容的文件名和 inode 的无序列表(这已被极大简化)。

It's probably reading them according to the order they're stored in the directory's list of files. On certain Unix-like filesystems, the directory is essentially an unordered list of filenames and inodes that point to the contents (this is tremendously simplified).

骑趴 2024-07-30 10:56:01

目录条目不是按排序顺序存储的,您不应该假设它们是这样存储的。 如果你想对它们进行排序,你就必须对它们进行排序。 例如,比较以下输出:

perl -e 'opendir DIR, "."; print join("\n", sort readdir(DIR)); print "\n";'

perl -e 'opendir DIR, "."; print join("\n", readdir(DIR)); print "\n";'

Directory entries are not stored in sorted order and you should not assume they're stored that way. If you want to sort them, you have to sort them. For example, compare the output of:

perl -e 'opendir DIR, "."; print join("\n", sort readdir(DIR)); print "\n";'

perl -e 'opendir DIR, "."; print join("\n", readdir(DIR)); print "\n";'
愛上了 2024-07-30 10:56:01

如果您的脚本使用 opendir() (直接或间接),则您无法假设有关其返回的文件的顺序的任何信息; 它将取决于您正在访问的操作系统和文件系统类型。 几个选项是:

  1. 使用两个循环:一个循环读取所有文件名,第二个循环按照您需要的顺序处理它们。
  2. 使用其他命令(例如调用“ls”)强制按您需要的顺序返回文件名。

If your script uses opendir() (directly or indirectly), you cannot assume anything about the ordering ordering of the files it returns; it will depend on the OS and type of filesystem you are accessing. A couple of options are:

  1. use two loops: one to read all the filenames, the second to process them in the order you require.
  2. use some other command (like invoking "ls") to force the filenames to be returned in the order you require.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文