Perl中的施瓦兹变换? [英] Schwartzian transform in Perl?
问题描述
my @output =
map $_->[0],
sort{$a->[1] <=> $b->[1]}
map [$_,-s $_],
@array;
有人能更详细地解释一下代码吗?我无法理解它的头或尾..
Can someone explain the code in more detail? I can't get head or tail of it ..
推荐答案
自下而上阅读:
@array
一个数组(文件名,稍后使用).
An array (of filenames, given later usage).
map [$_,-s $_],
对于每个文件名,获取对双元素匿名数组的引用,第一个元素是文件名,第二个元素是文件的字节大小.map 返回这些数组引用的列表.
For each filename, get a reference to a two element anonymous array, with the first element being the filename and the second element, the byte size of the file. map returns a list of these array references.
sort{$a->[1] <=> $b->[1]}
通过增加文件大小对数组引用列表进行排序.
Sort the list of array references by increasing file size.
map $_->[0],
将数组引用列表转回文件名列表,但现在按排序顺序.
Turn the list of array references back into a list of filenames, but now in sorted order.
my @output =
将列表保存在@output 中.
Save the list in @output.
这在功能上等同于:
my @output = sort { -s $a <=> -s $b } @array;
但只获取每个文件的大小一次,而不是每次通过排序进行比较时获取一次.
but only gets the size for each file once instead of once per comparison done by the sort.
这篇关于Perl中的施瓦兹变换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!