如何对 Data::Dumper 的输出进行排序?

发布于 2024-12-05 12:20:43 字数 107 浏览 3 评论 0原文

我想转储对象和散列的值,但它总是乱序打印键。如何按(递归)排序顺序转储键?

use Data::Dumper;
print Dumper $obj;

I want to dump the values of my object and hash, but it keeps printing the keys out of order. How can I dump the keys in (recursive) sort-order?

use Data::Dumper;
print Dumper $obj;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我不咬妳我踢妳 2024-12-12 12:20:43

设置 $Data::Dumper::Sortkeys = 1 以获取 Perl 的默认排序顺序。
如果要自定义顺序,请将 $Data::Dumper::Sortkeys 设置为对子例程的引用,该子例程接收对散列的引用作为输入,并输出对散列列表的引用键按照您希望它们出现的顺序排列。

# sort keys
$Data::Dumper::Sortkeys = 1;
print Dumper($obj);

# sort keys in reverse order - use either one
$Data::Dumper::Sortkeys = sub { [reverse sort keys %{$_[0]}] };
$Data::Dumper::Sortkeys = sub { [sort {$b cmp $a} keys %{$_[0]}] };
print Dumper($obj);

Set $Data::Dumper::Sortkeys = 1 to get Perl's default sort order.
If you want to customize the order, set $Data::Dumper::Sortkeys to a reference to a subroutine that receives a reference to a hash as input, and outputs a reference to the list of the hash's keys in the order you want them to appear.

# sort keys
$Data::Dumper::Sortkeys = 1;
print Dumper($obj);

# sort keys in reverse order - use either one
$Data::Dumper::Sortkeys = sub { [reverse sort keys %{$_[0]}] };
$Data::Dumper::Sortkeys = sub { [sort {$b cmp $a} keys %{$_[0]}] };
print Dumper($obj);
等风来 2024-12-12 12:20:43

对于不耐烦的人的简短回答

请使用 Data::Dumper::Concise 代替。它会对你的钥匙进行排序。像这样使用它:

use Data::Dumper::Concise;

my $pantsToWear = {
    pony       => 'jeans',
    unicorn    => 'corduroy',
    marsupials => {kangaroo => 'overalls', koala => 'shorts + suspenders'},
};

warn Dumper($pantsToWear);

为好奇的人提供更多单词

Data::Dumper::Concise 还为您提供更紧凑、更易于阅读的输出。

请注意,Data::Dumper::Concise Data::Dumper 为您设置了合理的默认配置值。它相当于像这样使用 Data::Dumper:

use Data::Dumper;
{
  local $Data::Dumper::Terse = 1;
  local $Data::Dumper::Indent = 1;
  local $Data::Dumper::Useqq = 1;
  local $Data::Dumper::Deparse = 1;
  local $Data::Dumper::Quotekeys = 0;
  local $Data::Dumper::Sortkeys = 1;
  warn Dumper($var);
}

Short answer for the impatient

Use Data::Dumper::Concise instead. It sorts your keys. Use it like this:

use Data::Dumper::Concise;

my $pantsToWear = {
    pony       => 'jeans',
    unicorn    => 'corduroy',
    marsupials => {kangaroo => 'overalls', koala => 'shorts + suspenders'},
};

warn Dumper($pantsToWear);

More words for the curious

Data::Dumper::Concise also gives you more compact, easier to read output.

Note that Data::Dumper::Concise is Data::Dumper with reasonable default configuration values set for you. Its equivalent to using Data::Dumper like this:

use Data::Dumper;
{
  local $Data::Dumper::Terse = 1;
  local $Data::Dumper::Indent = 1;
  local $Data::Dumper::Useqq = 1;
  local $Data::Dumper::Deparse = 1;
  local $Data::Dumper::Quotekeys = 0;
  local $Data::Dumper::Sortkeys = 1;
  warn Dumper($var);
}
流年已逝 2024-12-12 12:20:43

您可以将 $Data::Dumper::Sortkeys 变量设置为 true 值以获得默认排序:

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

my $hashref = {
    bob => 'weir',
    jerry =>, 'garcia',
    nested => {one => 'two', three => 'four'}};

print Dumper($hashref), "\n";

或者在其中放置一个子例程以根据需要对键进行排序。

You can set the $Data::Dumper::Sortkeys variable to a true value to get a default sort:

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

my $hashref = {
    bob => 'weir',
    jerry =>, 'garcia',
    nested => {one => 'two', three => 'four'}};

print Dumper($hashref), "\n";

or put a subroutine in there to sort the keys however you want.

痴梦一场 2024-12-12 12:20:43

来自 Data::Dumper 文档:

$Data::Dumper::Sortkeys or $OBJ->Sortkeys([NEWVAL])
Can be set to a boolean value to control whether hash keys are dumped in sorted order. 
A true value will cause the keys of all hashes to be dumped in Perl's default sort order. 
Can also be set to a subroutine reference which will be called for each hash that is dumped. 
In  this case Data::Dumper will call the subroutine once for each hash, passing it the 
reference of the hash. The purpose of the subroutine is to return a reference to an array of 
the keys that will be dumped, in the order that they should be dumped. Using this feature, you 
can control both the order of the keys, and which keys are actually used. In other words, this 
subroutine acts as a filter by which you can exclude certain keys from being dumped. Default is  
0, which means that hash keys are not sorted.

From the Data::Dumper documentation:

$Data::Dumper::Sortkeys or $OBJ->Sortkeys([NEWVAL])
Can be set to a boolean value to control whether hash keys are dumped in sorted order. 
A true value will cause the keys of all hashes to be dumped in Perl's default sort order. 
Can also be set to a subroutine reference which will be called for each hash that is dumped. 
In  this case Data::Dumper will call the subroutine once for each hash, passing it the 
reference of the hash. The purpose of the subroutine is to return a reference to an array of 
the keys that will be dumped, in the order that they should be dumped. Using this feature, you 
can control both the order of the keys, and which keys are actually used. In other words, this 
subroutine acts as a filter by which you can exclude certain keys from being dumped. Default is  
0, which means that hash keys are not sorted.
歌入人心 2024-12-12 12:20:43

对于那些在使用 Data::Dumper 打印时想要按值对 hashref 进行排序的人,这里有一个示例:

$Data::Dumper::Sortkeys = sub {
    # Using <=> to sort numeric values
    [ sort { $_[0]->{$a} <=> $_[0]->{$b} } keys %{ $_[0] } ]
};

这是一个更具可读性的替代方案,执行相同的操作,但是用一个变量来保存哈希值。它的效率较低,但对于较小的哈希值,有些人可能会发现它更好:

$Data::Dumper::Sortkeys = sub {
    my %h = %{$_[0]};
    # cmp for string comparisons
    [ sort { $h{$a} cmp $h{$b} } keys %h ];
};

For those who want to sort a hashref by value when printing it with Data::Dumper, here is an example:

$Data::Dumper::Sortkeys = sub {
    # Using <=> to sort numeric values
    [ sort { $_[0]->{$a} <=> $_[0]->{$b} } keys %{ $_[0] } ]
};

And here is a more readable alternative, doing the same but with a variable to hold the hash. It's less efficient, but for small hashes, some may find it nicer:

$Data::Dumper::Sortkeys = sub {
    my %h = %{$_[0]};
    # cmp for string comparisons
    [ sort { $h{$a} cmp $h{$b} } keys %h ];
};
倒数 2024-12-12 12:20:43

对 ascii 和全数字进行排序:

$Data::Dumper::Sortkeys = sub {
  no warnings 'numeric';
  if(join('',keys %{$_[0]})=~/\d+/)
  {
    [ sort { $a <=> $b } keys %{$_[0]} ]
  }
  else
  {
    return [sort(keys %{$_[0]})];
  }
};

sort ascii and full numeric:

$Data::Dumper::Sortkeys = sub {
  no warnings 'numeric';
  if(join('',keys %{$_[0]})=~/\d+/)
  {
    [ sort { $a <=> $b } keys %{$_[0]} ]
  }
  else
  {
    return [sort(keys %{$_[0]})];
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文