【发布时间】:2014-05-06 08:45:45
【问题描述】:
下面是我的哈希代码
#!/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" : []
}
]
}
【问题讨论】:
-
你试过什么?如果您发布一些代码以及运行该代码时获得的结果,您将获得更多帮助。只问“给我解决方案”不是一个好主意。
-
感谢保罗的建议。我试图遍历散列,然后将每个二级元素分配为子元素,但它没有按预期工作,而且我没有将散列分配给散列的想法。请帮忙。
-
预期的输出不是你想的那样。尝试 Data::Dumper 并转储值 - 哈希不能有多个同名键。
-
您要创建图表吗?
-
嗨@rahulsagar,您可以发布带有上述问题描述的实际代码,以便人们可以帮助您更轻松地调试它。
标签: perl hash perl-data-structures