Schwartzian transform in Perl?

前端 未结 2 1998
栀梦
栀梦 2020-12-17 02:04
my @output =
map $_->[0],
sort{$a->[1] <=> $b->[1]}
map [$_,-s $_],     
@array;   

Can someone explain the code in more detail? I c

相关标签:
2条回答
  • 2020-12-17 02:18

    Read from the bottom up:

    @array
    

    An array (of filenames, given later usage).

    map [$_,-s $_],
    

    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 =
    

    Save the list in @output.

    This is equivalent in function to:

    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.

    0 讨论(0)
  • 2020-12-17 02:27

    Wikipedia has a detailed explanation and analysis

    0 讨论(0)
提交回复
热议问题