Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Perl中使用sort和map按两列排序_Perl_Sorting_Perl Data Structures - Fatal编程技术网

在Perl中使用sort和map按两列排序

在Perl中使用sort和map按两列排序,perl,sorting,perl-data-structures,Perl,Sorting,Perl Data Structures,我的名单如下 @emprecords = ( ['pavan',24,25000], ['kumar',25,35000], ['ajay',22,35000], ['vijay',25,20000] ); 我需要先按最低年龄和最高工资对他们进行排序。使用进行数字比较,使用有条件的或检查年龄相等时的工资: #!/usr/bin/env perl us

我的名单如下

@emprecords = (
                  ['pavan',24,25000],
                  ['kumar',25,35000],
                  ['ajay',22,35000],
                  ['vijay',25,20000]
);
我需要先按最低年龄和最高工资对他们进行排序。

使用
进行数字比较,使用有条件的
检查年龄相等时的工资:

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my @emprecords = sort {
                $a->[1] <=> $b->[1]
                        or
                $b->[2] <=> $a->[2]
                }
                (  ['pavan',24,25000],
                  ['kumar',25,35000],
                  ['ajay',22,35000],
                  ['vijay',25,20000]
);

print Dumper \@emprecords;
这将产生:

$VAR1 = [
          [
            'ajay',
            22,
            35000
          ],
          [
            'pavan',
            24,
            25000
          ],
          [
            'kumar',
            25,
            35000
          ],
          [
            'vijay',
            25,
            20000
          ]
        ];

我们应该猜猜哪个字段是“年龄”,哪个字段是“slary”?实际上第二列是年龄,第三列是薪水..那么也许你应该写“薪水”而不是“slary”。我可以在排序中使用“or”吗?如果第一个比较(年龄)相等(意思是0),那么or开始按薪水进行比较。
$VAR1 = [
          [
            'ajay',
            22,
            35000
          ],
          [
            'pavan',
            24,
            25000
          ],
          [
            'kumar',
            25,
            35000
          ],
          [
            'vijay',
            25,
            20000
          ]
        ];