如何在Perl6中查看哈希的内容(以类似于Perl5模块Data::Dump或Data::Show的方式)?
在Perl 5中,如果我想查看散列的内容,我可以使用或 例如:如何在Perl6中查看哈希的内容(以类似于Perl5模块Data::Dump或Data::Show的方式)?,perl,hash,raku,Perl,Hash,Raku,在Perl 5中,如果我想查看散列的内容,我可以使用或 例如: use Data::Show; my %title_for = ( 'Book 1' => { 'Chapter 1' => 'Introduction', 'Chapter 2' => 'Conclusion', }, 'Book 2' => { 'Chapter 1' => 'Intro', 'Chapter
use Data::Show;
my %title_for = (
'Book 1' => {
'Chapter 1' => 'Introduction',
'Chapter 2' => 'Conclusion',
},
'Book 2' => {
'Chapter 1' => 'Intro',
'Chapter 2' => 'Interesting stuff',
'Chapter 3' => 'Final words',
}
);
show(%title_for);
哪些产出:
======( %title_for )======================[ 'temp.pl', line 15 ]======
{
"Book 1" => { "Chapter 1" => "Introduction", "Chapter 2" => "Conclusion" },
"Book 2" => {
"Chapter 1" => "Intro",
"Chapter 2" => "Interesting stuff",
"Chapter 3" => "Final words",
},
}
Perl 6中有什么等效的东西吗?我记得Damian Conway在YAPC 2010上讨论过这个功能,但后来我丢了笔记,谷歌也帮不上忙
use v6;
my %title_for = (
"Book 1" => { "Chapter 1" => "Introduction", "Chapter 2" => "Conclusion" },
"Book 2" => {
"Chapter 1" => "Intro",
"Chapter 2" => "Interesting stuff",
"Chapter 3" => "Final words",
},
);
%title_for.say;
我发现最接近工作的东西是%title\u。比如说
。但是,嵌套哈希看起来很混乱:
Book 1 => Chapter 1 => Introduction, Chapter 2 => Conclusion, Book 2 => Chapter 1 => Intro, Chapter 2 => Interesting stuff, Chapter 3 => Final words
我使用的是从上运行的Perl6。似乎建议在数据结构上使用.perl
,但看起来漂亮的打印需要额外的工作:
鉴于:
Perl 5
Perl 6
此后,我制作了一个可能很方便的模块:
use v6;
use PrettyDump;
my %title_for = (
"Book 1" => { "Chapter 1" => "Introduction", "Chapter 2" => "Conclusion" },
"Book 2" => {
"Chapter 1" => "Intro",
"Chapter 2" => "Interesting stuff",
"Chapter 3" => "Final words",
},
);
say PrettyDump.new.dump: %title_for;
输出:
Hash={
:Book 1(Hash={
:Chapter 1("Introduction"),
:Chapter 2("Conclusion")
}),
:Book 2(Hash={
:Chapter 1("Intro"),
:Chapter 2("Interesting stuff"),
:Chapter 3("Final words")
})
}
您也可以为PrettyDump
提供一些格式选项。用于rakudo中的数据转储
my @array_of_hashes = (
{ NAME => 'apple', type => 'fruit' },
{ NAME => 'cabbage', type => 'no, please no' },
);
dd @array_of_hashes;
这正是我想要记住的:
.perl
。谢谢
use v6;
use PrettyDump;
my %title_for = (
"Book 1" => { "Chapter 1" => "Introduction", "Chapter 2" => "Conclusion" },
"Book 2" => {
"Chapter 1" => "Intro",
"Chapter 2" => "Interesting stuff",
"Chapter 3" => "Final words",
},
);
say PrettyDump.new.dump: %title_for;
Hash={
:Book 1(Hash={
:Chapter 1("Introduction"),
:Chapter 2("Conclusion")
}),
:Book 2(Hash={
:Chapter 1("Intro"),
:Chapter 2("Interesting stuff"),
:Chapter 3("Final words")
})
}
my @array_of_hashes = (
{ NAME => 'apple', type => 'fruit' },
{ NAME => 'cabbage', type => 'no, please no' },
);
dd @array_of_hashes;