通过递归生成带有属性的无序列表

时间:2014-03-18 16:54:22

标签: perl recursion nested-lists

我必须生成一个数据哈希,使得相关节点的序列显示在无序列表中的节点名称旁边

my $hash =({
        node_hash =>{
                'Level1' =>{
                              'Level2a' =>{
                                            'Level3a' => 1,
                                            'Level3b' => 1,
                                           }
                              'Level2b' => {
                                              'Level3c' => 1,
                                            'Level3d' => 1,
                                           }

                              }

                      }
        'sequence' => {
                '10' =>{
                              '10.1' =>{
                                            '10.1.1' => 1,
                                            '10.1.1' => 1,
                                           }
                              '10.2' => {
                                            '10.2.1' => 1,
                                            '10.2.2' => 1,
                                           }

                              }

                      } 
                  });

是否可以将它们关联起来:无序列表具有结果:

<ul>
    <li sequence="10">Level1
                       <ul>
                              <li sequence="10.1">Level2a
                                      <ul>
                                            <li sequence ="10.1.1">Level3a</li>
                                            <li sequence ="10.1.2">Level3b</li>
                                      </ul>
                              </li>
                              <li sequence="10.2">Level2b
                                      <ul>
                                            <li sequence ="10.2.1">Level3c</li>
                                            <li sequence ="10.2.2">Level3d</li>
                                      </ul>
                              </li>
                        </ul>
            </li>
</ul>

现在我将散列发送到递归子例程,我可以生成字段列表,而没有序列属性:

unorder($hash->{node_hash});

sub unorder {
my ( $data, $level ) = ( @_, 0 );
my $tab    = '  ';
my $indent = $tab x $level;

print "$indent<ul>\n";
while ( my ( $key, $val ) = each %$data ) {
    print "$indent$tab<li level='".$level."'><a href='#'>$key</a>\n";
    make_ul( $val, $level + 1 ) if ref $val eq 'HASH';
    print "$indent</li>\n";
}
print "$indent</ul>\n";
} ## end sub unorder

1 个答案:

答案 0 :(得分:0)

这可以为您提供所需的内容:

sub unordlist {
    my $first = shift;
    my $second = shift;
    return '' unless ref $first eq "HASH";
    my @a = keys %$first;
    my @b = keys %$second;
    my $return = "<ul>";

    while (my $x = shift @a) {
        my $y = shift @b or die "keys are unbalanced";
        $return .= qq{<li sequence="$y">$x};
        $return .= unordlist ($first->{$x}, $second->{$y});
        $return .= "</li>";
    }
    $return .= "</ul>";

    $return;
}

unordlist ($hash->{ node_hash }, $hash->{ sequence });

我会保留格式化和理性变量名称。