web-dev-qa-db-ja.com

Data :: Dumperの出力をどのようにソートしますか?

オブジェクトとハッシュの値をダンプしたいのですが、キーが順番どおりに出力されません。キーを(再帰的な)ソート順でダンプするにはどうすればよいですか?

use Data::Dumper;
print Dumper $obj;
26
qodeninja

$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);
44
socket puppet

せっかちな人への短い答え

代わりに 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 :: ConciseisData :: 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);
}
11
Eric Johnson

から - 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.
5
user554546

$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";

または、そこにサブルーチンを配置して、必要に応じてキーを並べ替えます。

5
Alex

ハッシュ参照を値でソート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 ];
};
1
mivk

aSCIIと完全な数値を並べ替えます。

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