1

我想迭代以下多级哈希引用并创建一个显示父->子关系的单个选择框。以下是我从转储程序获得的哈希引用。所以基本上我想创建一个子程序,通过它我们可以构建一个 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>

提前致谢!

新哈希如下:

my $hash  = {
          '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'
                 }
        };

我需要我的输出如下

<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>

它应该如下所示:

Main Forum
add child, child of child
test
sub form under test forum, another child
Nokia C2-01
Android
under category

提前致谢。

4

1 回答 1

0

这将创建一个没有排序的 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);
于 2013-08-09T12:13:17.000 回答