是否可以保证将结果遍历在Perl中排序? [英] Is there any guarantee that results of globbing will be sorted in Perl?
问题描述
是否可以保证将从glob返回的文件名数组(例如< *>
)排序?
Is there any guarantee that the array of filenames returned from a glob (e.g. <*>
) will be sorted?
我找不到文档中提到的一种排序方式或另一种排序方式 ,但在我尝试过的每个目录中似乎都是这种情况.
I can't find that sorting is mentioned one way or the other in the documentation, but it seems to be the case in every directory I've tried it on.
我正在谈论使用这种语法:
I'm talking about using this syntax:
@files = <*>;
如果我需要对文件进行排序,以下内容是否多余?
If I need the files to be sorted, would the below be redundant?
@files = sort(<*>);
推荐答案
在Perl 5.6.0及更高版本中,文件名进行了排序:
In Perl 5.6.0 and newer, filenames are sorted:
从v5.6.0开始,此运算符使用标准实施File :: Glob扩展名.
Beginning with v5.6.0, this operator is implemented using the standard File::Glob extension.
默认情况下,路径名已排序ASCII升序.
By default, the pathnames are sorted in ascending ASCII order.
有一个收获:
默认情况下,假定文件名是区分大小写
By default, file names are assumed to be case sensitive
说了这么多,您可以更改此行为,以区分大小写使用
Having said all that, you can change this behavior to sort case-insensitively with
use File::Glob qw(:globally :nocase);
请注意::globally自5.6.0起是多余的,但这也适用于旧版本.
Note that :globally is redundant since 5.6.0, but this will work on older versions as well.
或者,如果您只想做一个不区分大小写的glob:
Alternately, if you just want to do a single glob with case-insensitivity:
use File::Glob ':glob';
@files = bsd_glob('*', GLOB_NOCASE);
这篇关于是否可以保证将结果遍历在Perl中排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!