带引用的perl sort 2d数组

时间:2015-05-18 14:30:33

标签: arrays perl sorting

我对perl有些新意,所以请耐心等待。到目前为止,我已经用尽了所有可能的解决方案。

假设我有一些帽子,其中一些测量值在其他地方填充。我想根据某个专栏对它们进行排序。我尝试使用perl的“排序”来做这个,但我不让它们实际排序。我相信问题是我对引用感到困惑。下面的代码就是我目前正在使用的代码。

INSERT  INTO License
        ( CompanyID ,
          LicenseStart ,
          LicenseEnd ,
          CreatedDate ,
          AddUser ,
          UpdateUser ,
          UpdateDate ,
          TemplateId
        )
        SELECT  ID ,
                GETDATE() ,
                DATEADD(DAY, 14, GETDATE()) ,
                NULL ,
                NULL ,
                NULL ,
                NULL ,
                NULL
        FROM    Company c
                LEFT JOIN License l ON l.CompanyID = c.ID
        WHERE   l.ID IS NULL

截至目前,它打印出未排序的数组值:

my @hat1 = [3, 4, 5, 6, 7, 8];
my @hat2 = [4, 6, 5, 1, 1, 2];
my @hat3 = [9, 8, 9, 3, 4, 4];
#eventually work with unknown number of hats

my @binToSort = (\@hat1,\@hat2,\@hat3);

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

for my $ref (@binSorted){
    for my $inner (@$ref){
        print "@$inner\n";
    }
}

但我希望能够到达:

3 4 5 6 7 8
4 6 5 1 1 2
9 8 9 3 4 4

我觉得这是一个简单的问题,但我无法找到正确的方法。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

你需要:

my $hat1 = [ 3, 4, 5, 6, 7, 8 ];
my $hat2 = [ 4, 6, 5, 1, 1, 2 ];
my $hat3 = [ 9, 8, 9, 3, 4, 4 ];

#eventually work with unknown number of hats

my @binToSort = ( $hat1, $hat2, $hat3 );

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

for my $ref (@binSorted) {
    for my $inner ( @{$ref} ) {
        print "$inner";
    }
    print "\n";
}

或者

my @hat1 = ( 3, 4, 5, 6, 7, 8 );
my @hat2 = ( 4, 6, 5, 1, 1, 2 );
my @hat3 = ( 9, 8, 9, 3, 4, 4 );

#eventually work with unknown number of hats

my @binToSort = ( \@hat1, \@hat2, \@hat3 );

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

for my $ref (@binSorted) {
    for my $inner ( @{$ref} ) {
        print "$inner";
    }
    print "\n";
}