我有一个具有父子关系的多级哈希引用,我想将该哈希转换为具有所有关系的简单 HTML 表。我的哈希看起来像:
$VAR1 = {
'4' => {
'forumid' => '136720',
'children' => {
'7' => {
'forumid' => '136997',
'title' => 'under category',
'is_category' => '0',
'parentid' => '136720'
}
},
'title' => 'Android',
'is_category' => '0',
'parentid' => '-1'
},
'1' => {
'forumid' => '136666',
'children' => {
'5' => {
'forumid' => '136954',
'children' => {
'8' => {
'forumid' => '137004',
'title' => 'child of child',
'is_category' => '0',
'parentid' => '136954'
}
},
'title' => 'add child',
'is_category' => '0',
'parentid' => '136666'
}
},
'title' => 'Main Forum',
'is_category' => '0',
'parentid' => '-1'
},
'3' => {
'forumid' => '136719',
'title' => 'Nokia C2-01',
'is_category' => '1',
'parentid' => '-1'
},
'2' => {
'forumid' => '136665',
'children' => {
'6' => {
'forumid' => '136994',
'children' => {
'9' => {
'forumid' => '137012',
'title' => 'another child',
'is_category' => '0',
'parentid' => '136994'
}
},
'title' => 'sub form under test forum',
'is_category' => '0',
'parentid' => '136665'
}
},
'title' => 'test',
'is_category' => '0',
'parentid' => '-1'
}
};
我正在使用以下函数来创建一些 HTML 表:
sub adminList {
my $hash = shift;
my $options = '';
my $iter;
$options .= "<table id='sortable' class='grid' >
<tbody>";
$iter = sub {
my $hash = shift;
my $indent = shift || '';
foreach my $k (sort keys %{$hash}) {
my $v = $hash->{$k};
$options .= "<tr><td>$v->{title}" ;
if($indent){
$options .= "<table id='sort'>
<tbody>
<tr>
<td> $indent $v->{title}</td>
</tr>
</tbody>
</table>
";
}
if ($v->{children}){
$iter->($v->{children}, $indent . "--");
}
$options .= "</td></tr>";
}
};
$iter->($hash);
$options .="</tbody></table>";
return $options;
}
这并没有给我想要的结果并重复两次标题而不是建立父子关系。输出应该像表格形式:
Main Forum
-- add child
---- child of child
test
-- sub form under test forum
---- another child
Nokia C2-01
Android
-- under category
我无法理清我所缺少的东西?任何帮助,将不胜感激。创建的表应如下所示:
<table id="sortable" class="grid" >
<tbody>
<tr>
<td><label>house-Five</label>
<table id="sort">
<tbody>
<tr>
<td>-- child1111</td>
</tr>
<tr>
<td><label>-- child22222</label>
<table id="sort">
<tbody>
<tr><td>---- first child of child22222</td></tr>
<tr>
<td>---- second child of child22222
<table id="sort">
<tbody>
<tr><td>------ first child of second child222222</td></tr>
<tr><td>------ second child of child22222222</td></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>-- child33333</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr><td>Psition four</td></tr>
<tr><td>catch me</td></tr>
<tr><td>Champions at six</td></tr>
<tr><td>Rosewater</td></tr>
</tbody>
</table>