產生深哈希映射下面是我的代碼以哈希在Perl
#!/usr/bin/perl
use warnings;
use JSON::PP; # Just 'use JSON;' on most systems
my %name = (
'sl' => {
'fsd' => {
'conf' => {
'ul' => '/sl/fsd/conf/ul',
'si' => '/sl/fsd/conf/si',
'ho1' => '/sl/fsd/conf/ho1'
}
}
},
're' => {
'fsd' => {
'cron' => {
'README' => '/re/fsd/cron/README'
},
'bin' => {
'db' => {
'smart.p_add_tag' => '/re/fsd/bin/db/smart.p_add_tag',
'smart.p_tag_partition' => '/re/fsd/bin/db/smart.p_tag_partition',
'smart.p_add_tag_type' => '/re/fsd/bin/db/smart.p_add_tag_type'
}
},
'doc' => {
'SMART' => '/re/fsd/doc/SMART',
'README' => '/re/fsd/doc/README'
},
'data' => {
'README' => '/re/fsd/data/README'
},
'conf' => {
'al1' => '/re/fsd/conf/al1',
'file' => '/re/fsd/conf/file',
'ho' => '/re/fsd/conf/ho',
'al3' => '/re/fsd/conf/al3',
'hst' => '/re/fsd/conf/hst',
'us' => '/re/fsd/conf/us',
'README' => '/re/fsd/conf/README',
'al2' => '/re/fsd/conf/al2'
}
}
}
);
(my $root) = keys %name;
my %nodes =();
my %tree =();
my @queue = ($root);
list_children(\%name, \@queue, \%nodes) while @queue;
my $tree = build_tree($root, \%nodes);
my $json = JSON::PP->new->pretty; # prettify for human consumption
print $json->encode($tree);
sub list_children {
my $adjac = shift;
my $queue = shift;
my $nodes = shift;
my $node = shift @$queue;
my @children = keys %{$adjac->{$node}};
@children = grep { ! exists $nodes->{$_}} @children;
$nodes->{$node} = \@children;
push @$queue, @children;
}
sub build_tree {
my $root = shift;
my $nodes = shift;
my @children;
for my $child (@{$nodes->{$root}}) {
push @children, build_tree($child, $nodes);
}
my %h = ('text' => $root,
'children' => \@children);
return \%h;
}
我試圖輸出JSONified哈希值,但它僅穿越最多兩個級別。而我需要它遍歷每個父代的最後一個子節點。有人可以幫助實現這一目標。
下面是電流輸出
{
"text" : "sl",
"children" : [
{
"text" : "fsd",
"children" : []
}
]
}
你試過了什麼?如果您在運行代碼時發佈了一些代碼和結果,您將獲得更多幫助。只是問'給我解決方案'不是一個好主意。 –
謝謝Paul的建議。 我試圖遍歷散列,然後分配每個第二級元素作爲孩子,但它沒有按預期工作,我想出了一個哈希散列的想法。請幫忙。 – rahulsagar
預期的產出不是你想象的。嘗試Data :: Dumper並轉儲值 - 一個哈希不能有多個同名的鍵。 – choroba