简体   繁体   English

如何在 perl 中删除 Data::Dumper 中每个唯一编号的标题名称?

[英]How can I remove the title name of each unique number in Data::Dumper in perl?

I use Data::Dumper to catch uniqe number in each element.我使用 Data::Dumper 来捕获每个元素中的唯一编号。

#!perl

use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

my @names = qw(A A A A B B B C D);
my %counts;
$counts{$_}++ for @names;
print Dumper (\%counts);

exit;

This is output.这是输出。

$VAR1 = {
          'A' => 4,
          'B' => 3,
          'C' => 1,
          'D' => 1
        };

How can I remove the title name of each unique number to get output like this format?如何删除每个唯一编号的标题名称以获得这种格式的输出?

$VAR1 = { 4 ,3 ,1 ,1 }

Presuming you want the counts in descending order, you could use the following:假设您希望按降序排列计数,您可以使用以下命令:

printf "\$VAR1 = { %s};\n",
   join ',', 
      map "$_ ",
         sort { $b <=> $a }
            values(%counts);

If instead you want the counts sorted by key,相反,如果您想要按键排序的计数,

printf "\$VAR1 = { %s};\n",
   join ',', 
      map "$counts{$_} ",
         sort
            keys(%counts);

Either way, that's a really weird format.无论哪种方式,这都是一种非常奇怪的格式。 Square brackets would make more sense than curly ones.方括号比花括号更有意义。

One of many ways to get desired result获得所需结果的众多方法之一

use strict;
use warnings;

use feature 'say';

my @names = qw( A A A A B B B C D );

my %counts;

$counts{$_}++ for @names;

my @values = map { $counts{$_} } sort keys %counts;

say join(',', @values);

output输出

4,3,1,1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM