30

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

use Data::Dumper;
print Dumper $obj;
4

6 回答 6

56

设置$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);
于 2011-09-19T05:40:54.613 回答
13

不耐烦的简短回答

请改用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::ConciseData::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);
}
于 2011-09-19T08:18:50.197 回答
6

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.
于 2011-09-19T05:42:00.233 回答
6

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

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

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

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

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

于 2011-09-19T05:44:51.157 回答
2

对 ascii 和完整数字进行排序:

$Data::Dumper::Sortkeys = sub {
  no warnings 'numeric';
  if(join('',keys %{$_[0]})=~/\d+/)
  {
    [ sort { $a <=> $b } keys %{$_[0]} ]
  }
  else
  {
    return [sort(keys %{$_[0]})];
  }
};
于 2019-06-18T16:35:37.593 回答
1

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

$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 ];
};
于 2016-01-09T23:06:44.237 回答