How can I sort an array or table by column in Perl?

前端 未结 3 2062
孤独总比滥情好
孤独总比滥情好 2020-12-18 06:10

I\'ve been looking everywhere for an answer to this, and I just can\'t get it to work.

I have an input file that is read into an array using Perl. The file is a text

3条回答
  •  死守一世寂寞
    2020-12-18 06:14

    In your last code example you can replace

    my @sorted = sort { $b->[4] <=> $a->[4] } @input;
    

    with

    my @sorted = sort { (split(' ', $b))[4] <=> (split(' ', $a))[4] } @input;
    

    or even

    my @sorted = sort { (split(/\s+/, $b))[4] <=> (split(/\s+/, $a))[4] } @input;
    

    if input data has no lines with leading spaces.

提交回复
热议问题