perl中的多级散列引用

时间:2013-08-14 11:46:44

标签: perl hash

我想在多级散列引用之后迭代,并希望创建parent->子关系以及拖放功能。以下是我从dumper获取的散列引用。所以我想创建一个部分并显示所有父级和子关系信息,我可以将子项拖放到其父元素中,也可以拖放每个父元素以更改显示顺序。但我首先要正确显示parent->子关系。

哈希如下:

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>

因此应该看起来应该将child添加到用逗号分隔的父元素中:

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

这是创建哈希的函数:

     sub subforumsTree {
    my $forums = shift; 
    my $pid = shift || ('-1' || '0');
    my $HASH;   
    foreach my $forum ( @{$forums}){
        #next if ($forum->{is_category});           
        if($forum->{parentid} eq $pid){
            $HASH->{$forum->{displayorder}} = {forumid=>$forum->{forumid},parentid=>$forum->{parentid},title=>$forum->{title},is_category=>$forum->{is_category}};  
            my $children =  &subforumsTree( $forums,$forum->{forumid} );
            if( $children ) {
               $HASH->{$forum->{displayorder}}->{'children'} = $children;
            }
        }
    }

     return $HASH;
}

here is the function which I tried for displaying elements:
sub adminforumsList {

    my $hash = shift;
    my $options = '';
    my $iter; 
    $iter = sub {
        my $hash = shift;
        my $indent = shift || '';
        foreach my $k (sort keys %{$hash}) {  
            my $v = $hash->{$k};
                        $options .= "<div id=cat_".$v->{forumid}." class=isDraggable>
                        <div class='col_buttons right' style='white-space: nowrap'>
                        <ul class='ipsControlStrip'>
                        <li class='i_add'>
                        <a href='#' title='New Forum...'>New Forum...</a>
                        </li>
                        <li class='i_edit'>
                        <a href='#' title='Edit Settings...'>Edit Settings...</a>
                        </li>
                        <li class='ipsControlStrip_more ipbmenu' id='menum-26'>
                        <a href='#'>Options</a>
                        </li>
                        </ul>
                        <ul class='acp-menu' id='menum-26_menucontent' style='display: none'>
                        <li class='icon info'><a href='#'>Permissions...</a></li>
                        <li class='icon delete'><a href='#'>Delete Category...</a></li>
                        <li class='icon edit'><a href='#'>Edit as Forum...</a></li>
                        <li class='icon view'><a href='#'>Skin Options...</a></li>
                        </ul>
                        </div><div class='draghandle'>&nbsp;</div>
                        <div class='item_info'>
                        <img src='/images/icons/folder.png' />
                        &nbsp;&nbsp;<strong class='larger_text'>$v->{title}</strong>                
                        </div>";
                        $options .= "<div id='cat_wrap_".$v->{forumid}. "' class='item_wrap'>";
                        $options .= "<div class='item ipsControlRow isDraggable' id='forum_".$v->{forumid}."'>";
                        $options .= "<table style='width: 100%'>
                        <tr>
                        <td style='width: 20px !important; vertical-align: top;'>
                            <div class='draghandle'>&nbsp;</div>
                        </td>
                        <td style=''>
                            <div class='item_info'>
                                <strong class='forum_name'>$v->{title}</strong>
                                <br />
                                <span class='desctext'>$v->{description}</span>
                            </div>
                            </td>                           
                        </tr>
                        </table></div></div></div>";

            if ($v->{children}){
                $iter->($v->{children}, $indent . "--");
            }
        }
    };
    $iter->($hash);
    return $options;
}

1 个答案:

答案 0 :(得分:0)

您没有显示您尝试过的内容,而您的问题并不完全清楚,这可能意味着您的问题将被关闭。

我不会为你做太多编程,但我建议你研究Perl Object Oriented Programming

每个论坛都可以是对象,其中一个字段可以是其他论坛的哈希值。如果您已经了解了引用,那么编写OO Perl就不那么难了。通过将每个论坛定义为论坛类的成员,您可以删除在散列哈希值上操作的棘手部分。

我还建议你将问题分解成更小的部分。一旦掌握了代码的基础知识,就可以添加拖放功能。你想输出XML吗?目前还不清楚你要做什么。如果您只想存储结构,可以使用Storable。如果你将它传递给你的论坛结构,它会将整个结构存储在一个文件中,你将能够重新读取它。对于这些类型的任务,可存储比XML更容易使用,它是一个标准模块。

但是,如果您必须使用XML,则应使用Perl中提供的众多XML modules之一