对行数组的引用进行排序,其中每行都存储为哈希

Posted

技术标签:

【中文标题】对行数组的引用进行排序,其中每行都存储为哈希【英文标题】:Sorting a reference to an array of rows where each row is stored as a hash 【发布时间】:2022-01-20 02:39:40 【问题描述】:

我正在尝试在 Perl 中按 location_id 对以下数据结构进行排序。

my $employees = $dbh->selectall_arrayref(qq[
    SELECT name, type, code, emp_cat_id,
           percentage, location_id
    FROM table_1
], Slice =>  );
 
for my $row (@$employees) 
   push @
      $args->employees $row->emp_cat_id 
   , $row;

例子:

123 => [
   
      percentage  => 0.25,
      code        => "XYZ",
      name        => "John Doe",
      type        => "pt",
      location_id => 001,
      emp_cat_id  => 123

   
],
555 => [
   
      percentage  => 0.50,
      code        => "ZZZ"
      name        => "Chris Cringle",
      type        => "ft",
      location_id => 007,
      emp_cat_id  => 555

   ,
   
      percentage  => 0.25,
      code        => "XXX"
      name        => "Tom Thompson",
      type        => "pt",
      location_id => 002,
      emp_cat_id  => 555

   
]

对于每个 emp_cat_id,我需要结构按升序排列 location_id。

我尝试了以下方法,但出现“在第 # 行的 void 上下文中无用使用排序”或“在第 # 行的标量上下文中无用排序”错误。

$args->employees = sort 
   $a->location_id <=> $b->location_id
 $args->employees;

感谢您对理解排序的任何帮助!

【问题讨论】:

【参考方案1】:

问题是您正在对emp_cat_id555 的数组(ref) 进行排序,然后是123,因此需要取消引用以对这些arrayrefs 进行排序。所以

foreach my $id (keys $args->employees)  
    @ $args->employees$id  = sort  
            $a->location_id <=> $b->location_id 
        
        @ $args->employees$id  

(用问题中显示的结构测试,这里省略)

这样做会将007 丢失为7。这当然可以解决,如果有问题请告诉我。

如果您真的只有密钥 employees,请考虑提取 $args-&gt;employees hashref 并使用它。会容易很多

use Storable qw(dclone);

my $employees = dclone $args->employees;  # need deep copy

哦,这就是全部内容

use warnings;
use strict;
use feature 'say';
use Data::Dump qw(dd);

my $args = 
    employees => 
        123 => [
            
                percentage  => 0.25,
                code        => "XYZ",
                name        => "John Doe",
                type        => "pt",
                location_id => 001,
                emp_cat_id  => 123
            
        ],
        555 => [
            
                percentage  => 0.50,
                code        => "ZZZ",
                name        => "Chris Cringle",
                type        => "ft",
                location_id => 007,
                emp_cat_id  => 555

            ,
            
                percentage  => 0.25,
                code        => "XXX",
                name        => "Tom Thompson",
                type        => "pt",
                location_id => 002,
                emp_cat_id  => 555

            
        ]
    
;

foreach my $id (keys $args->employees) 
    @ $args->employees$id  = sort 
            $a->location_id <=> $b->location_id
        
        @ $args->employees$id 


dd $args;

【讨论】:

【参考方案2】:

那么,你有一个 hashref,其中每个元素都是 hashref 的 arrayref,应该根据 hashref 内部的键进行排序?

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my $hashref = 
    123 => [
        
            percentage  => 0.25,
            code        => "XYZ",
            name        => "John Doe",
            type        => "pt",
            location_id => 001,
            emp_cat_id  => 123
        
        ],
    555 => [
        
            percentage  => 0.50,
            code        => "ZZZ",
            name        => "Chris Cringle",
            type        => "ft",
            location_id => 007,
            emp_cat_id  => 555
        ,
        
            percentage  => 0.25,
            code        => "XXX",
            name        => "Tom Thompson",
            type        => "pt",
            location_id => 002,
            emp_cat_id  => 555
        
        ]
;

foreach my $arrayref (values %$hashref) 
    @$arrayref = sort  $a->location_id <=> $b->location_id  @$arrayref;


print Dumper($hashref);

您缺少的重要部分是取消引用 arrayrefs。 @$arrayref 而不仅仅是 $arrayref

【讨论】:

以上是关于对行数组的引用进行排序,其中每行都存储为哈希的主要内容,如果未能解决你的问题,请参考以下文章

按自定义数组对行进行排序

Access 2010 SQL--在交叉表查询中按聚合函数对行进行排序

《程序设计基础》实验题目1 二维数组 冒泡排序

java语言基础知识

Bootstrap jquery sortable 只对行进行排序?

使用 NSPredicate 对 NSArray 进行排序,然后存储在一个新数组中