Html 遍历多级散列引用

Html 遍历多级散列引用,html,perl,Html,Perl,我想迭代下面的多级hash ref并创建一个显示父->子关系的选择框。所以基本上我想创建一个子例程,通过它我们可以构建一个HTML选择框 my $hash = { '136666' => { 'children' => { '136954' => {

我想迭代下面的多级hash ref并创建一个显示父->子关系的选择框。所以基本上我想创建一个子例程,通过它我们可以构建一个HTML选择框

my $hash = {
          '136666' => {
                        'children' => {
                                        '136954' => {
                                                      'children' => {
                                                                      '137004' => {
                                                                                    'title' => 'child of child',
                                                                                    'parentid' => '136954'
                                                                                  }
                                                                    },
                                                      'title' => 'add child',
                                                      'parentid' => '136666'
                                                    }
                                      },
                        'title' => 'Main Forum',
                        'parentid' => '-1'
                      },
          '136720' => {
                        'children' => {
                                        '136997' => {
                                                      'title' => 'under category',
                                                      'parentid' => '136720'
                                                    }
                                      },
                        'title' => 'Android',
                        'parentid' => '-1'
                      },
          '136719' => {
                        'title' => 'Nokia C2-01',
                        'parentid' => '-1'
                      },
          '136665' => {
                        'children' => {
                                        '136994' => {
                                                      'title' => 'sub form under test forum',
                                                      'parentid' => '136665'
                                                    }
                                      },
                        'title' => 'test',
                        'parentid' => '-1'
                      }
        };
并希望选择如下框:

<option value='136666'>Main Forum</option>
<option value='136954'>--add child</option>
<option value='137004'>----child of child'</option>
<option value='136665'>test</option>
<option value='136994'>--sub form under test forum</option>
<option value='136719'>Nokia C2-01</option>
<option value='136720'>--Android</option>
<option value='136997'>----under category'</option>
我需要我的输出如下

<div id='136666'>Main Forum
    <div>
        <a href='136954'>add child</a>, <a href='137004'>child of child</a>
    </div>
</div>
<div id='136665'>test
    <div>
        <a href='136994'>sub form under test forum</a>, <a href='137012'>another child</a>
    </div>  
</div>
<div id='136719'>Nokia C2-01
    <div id='136720'>Android
        <div>
            <a href='136997'>under category</a> 
        </div>  
    </div>  
</div>

提前谢谢

这将创建一个没有排序的HTML表单

use strict;
use warnings;
use Data::Dumper;

sub form {
    my $hash = shift;
    my $form = '';
    my $iter; $iter = sub {
        my $hash = shift;
        my $indent = shift || '';
        while(my($k, $v) = each %{$hash}) {
            $form .= "<option value='" . $k . "'>" . $indent . $v->{title} . "</option>\n";
            if ($v->{children}){
                $iter->($v->{children}, $indent . "-");
            }
        }
    };
    $iter->($hash);
    return $form;
}

##EDIT
##SORT options by id
sub Sortedform {
    my $hash = shift;
    my $form = '';
    my $iter; $iter = sub {
        my $hash = shift;
        my $indent = shift || '';
        foreach my $k (sort keys %{$hash}) {
            my $v = $hash->{$k};
            $form .= "<option value='" . $k . "'>" . $indent . $v->{title} . "</option>\n";
            if ($v->{children}){
                $iter->($v->{children}, $indent . "--");
            }
        }
    };
    $iter->($hash);
    return $form;
}

print Dumper form($hash);
print Dumper Sortedform($hash);

你希望它如何排序?欢迎,这就是为什么我问你是否希望它排序,请参阅上面的编辑。再次感谢,但现在我必须更改我的方法,并且必须更改我的哈希引用,因为我必须维护显示顺序,所以将哈希键更改为显示顺序。请查看上面的新哈希和所需输出。
Main Forum
add child, child of child
test
sub form under test forum, another child
Nokia C2-01
Android
under category
use strict;
use warnings;
use Data::Dumper;

sub form {
    my $hash = shift;
    my $form = '';
    my $iter; $iter = sub {
        my $hash = shift;
        my $indent = shift || '';
        while(my($k, $v) = each %{$hash}) {
            $form .= "<option value='" . $k . "'>" . $indent . $v->{title} . "</option>\n";
            if ($v->{children}){
                $iter->($v->{children}, $indent . "-");
            }
        }
    };
    $iter->($hash);
    return $form;
}

##EDIT
##SORT options by id
sub Sortedform {
    my $hash = shift;
    my $form = '';
    my $iter; $iter = sub {
        my $hash = shift;
        my $indent = shift || '';
        foreach my $k (sort keys %{$hash}) {
            my $v = $hash->{$k};
            $form .= "<option value='" . $k . "'>" . $indent . $v->{title} . "</option>\n";
            if ($v->{children}){
                $iter->($v->{children}, $indent . "--");
            }
        }
    };
    $iter->($hash);
    return $form;
}

print Dumper form($hash);
print Dumper Sortedform($hash);