【问题标题】:Perl - How to sort array values by a given order?Perl - 如何按给定顺序对数组值进行排序?
【发布时间】:2023-04-01 22:18:01
【问题描述】:

在那How to sort a list with a given order?

我们已经讨论了如何使用 map 和 $_ 根据给定的顺序对列表进行排序。今天我还有一个问题。

我有同样的排序:

my @orderby = ( 'car', 'boat', 'chicken', 'cat', 'dog', 'mouse');
# or if it's better to the code:
my %orderby = ( 'car' => 0, 
                'boat' => 1, 
                'chicken' => 2, 
                'cat' => 3, 
                'dog' => 4, 
                'mouse' => 5); 

现在我有以下需要通过 orderby 订购:

print Dumper \%toys;
$VAR = {
         'animals'  =>  [
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ],
                          [
                            'bald', 'bark', 'dog', 'black stripes'
                          ],
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ]
                        ],
         'notanima' =>  [
                          [
                            'paited', 'motor', 'boat', 'red'
                          ],
                          [
                            'painted', 'motor', 'car', 'blue on top'
                          ]
                        ]
       };

代码需要使用基于orderby的3列进行排序。您需要对动物和 notanima 使用相同的命令。 重新排列后,$VAR 将是:

$VAR = {
         'animals'  =>  [
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ],
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ],
                          [
                            'bald', 'bark', 'dog', 'black stripes'
                          ]
                        ],
         'notanima' =>  [
                          [
                            'painted', 'motor', 'car', 'blue on top'
                          ],
                          [
                            'paited', 'motor', 'boat', 'red'
                          ]
                        ]
       }; 
order %toys{key} by orderby;

我已尝试更改@ikegami 提供的地图解决方案

my %counts; ++$counts{$_} for @list;
my @sorted = map { ($_) x ($counts{$_}||0) } @orderby;

但我没有成功。 你们有什么想法我该如何实现这个目标?

提前谢谢!

更新!
我试图使用 ikegami 的建议,我已经这样做了:

# that first foreach will give one ARRAY for animals and one ARRAY for notanima
foreach my $key (keys %toys)
{
   # that one will give me access to the ARRAY referenced by the $key.
   foreach my $toy_ref ($toys{$key})
   {
       my %orderby = map {$orderby[$_] => $_} 0..$#orderby;
       my @sorted = sort { $orderby{$a} <=> $orderby{$b} } @{$toy_ref};
       # my @sorted = sort { $orderby{$a} <=> $orderby{$b} } $toy_ref;
       print Dumper @sorted;
   }
}

首先,这给了我警告:

Use of uninitialized value in numeric comparison (<=>) at....

还有notanima的排序结果(我会忽略动物,所以帖子不会那么大):

$VAR1 = [
         'paited', 'motor', 'boat', 'red'
       ];
$VAR2 = [
         'painted', 'motor', 'car', 'blue on top'
       ];

基于orderby,打印顺序需要为:

$VAR1 = [
         'painted', 'motor', 'car', 'blue on top'
       ];
$VAR2 = [
         'paited', 'motor', 'boat', 'red'
       ];

汽车需要先到。 我做错了什么?

【问题讨论】:

  • 如果之前的回答无效,请不要再问同样的问题;更新您的旧问题并取消标记答案。
  • 你为什么使用标有“弄乱人的脑袋”的解决方案?
  • 嘿@ikegami,谢谢你的回答。我使用了“人们头脑中的混乱”,因为字符串有数字和特殊字符。地图“”给我一个异常。 Robert P. 之前的帖子我没有改,因为问题和问题不同,但解决方案是相似的。我使用了与上一篇文章相同的字符串。

标签: perl hashtable


【解决方案1】:

所以您有 6 个数组要排序,因此您需要对 sort 进行 6 次调用。

  1. 对于 %toys 的每个元素,
    1. 对于由 %toys 的元素引用的数组的每个元素,
      1. 如前所示对引用的数组进行排序。

请不要使用标有“扰乱人心”的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多