构建“内省”嵌套html表

时间:2010-11-04 11:08:23

标签: perl recursion nested-table

这是我在Perl中使用Template Toolkit编写的内容,但它更像是一般算法问题。我的基本问题是给定这样的数据结构:

my @array = (
    [qw /00 01/],
    [qw /10/],
    [qw /20 21/],
    [qw /30 31 32 33 /],
);

我需要像这样的输出(为了说明而简化):

<00>
  <10>
    <20> <30>(00/10/20/30)</30> <31>(00/10/20/31)</31>
         <32>(00/10/20/32)</32> <33>(00/10/20/30)</33>
    </20>
    <21> <30>(00/10/21/30)</30> <31>(00/10/21/31)</31>
         <32>(00/10/21/31)</32> <33>(00/10/21/31)</33>
    </21>
  </10>
</00>
<01>
  <10>
    <20> <30>(01/10/20/30)</30> <31>(01/10/20/31)</31>
         <32>(01/10/20/32)</32> <33>(01/10/20/33)</33>
    </20>
    <21> <30>(01/10/21/30)</30> <31>(01/10/21/31)</31>
         <32>(01/10/21/32)</32> <33>(01/10/21/33)</33>
    </21>
</10>
</01>

这是作为实际输出的嵌套html表的简化示例。中心节点处的路径实际上是要调用另一个子例程以使用数据填充嵌套表的参数。我相当确定原始数组结构的转置是有用的,所以我写了Array::Transpose::Ragged并在今天早些时候在CPAN上发布了它。

我管理了一个实现,它从内到外构建嵌套结构(使用perl的Template Toolkit - 见下文),但是当我到达结构的外部部分时,我不再有机会在中央节点填充所需的数据。这是实现它的价值:

[% SET inner = "(path data should go here)" %]
[% MACRO process_groups(line, inner) BLOCK %]
[% FOREACH l IN line %]
<[% l %]>[% inner %]</[% l %]>
[% END %]
[% END %]
[% WHILE (x = records.pop) %]
[% inner = process_groups(x, inner) %]
[% END %]
[% inner %]

对于我应该采取的方法提出任何建议

更新:

为了兴趣,我想我已经接受了接受的答案的TT版本。有点棘手,因为TT不像perl那么灵活,但是这里有:

#!/usr/bin/env perl
use warnings;
use strict;
use Template;
my $template = Template->new();
my @array = (
    [ qw/00 01/ ], [ qw/10/ ],[ qw/20 21/ ], [ qw/30 31 32 33/ ]);
my $stash = { records => \@array, };
$template->process(\*DATA, $stash) || die $template->error(), "\n";

__END__
[% MACRO print_output(data, path_elements) BLOCK; %]
[% current = data.0; remaining = data.slice(1); %]
[% FOREACH d IN current %]
<[% d %]>
[% IF remaining.size > 0  %]
[% path_elements.push(d); print_output(remaining, path_elements); %]
[% SET discard = path_elements.pop %]
[% ELSE %]
([% path_elements.join('/')  _ '/' _ d  %])
[% END %]
</[% d %]>
[% END %]
[% END %]
[% SET path = []; print_output(records, path) %]

这里更好的是TT中的实际嵌套表结构:

[% MACRO print_output(data, path_elements) BLOCK; %]
<table> <tr>
[% current = data.0; remaining = data.slice(1); %]
[% FOREACH d IN current %]
<th>[% d %]</th>
[% END %] </tr>
<tr>
[% FOREACH d IN current %]
[% IF remaining.size > 0  %]
<td id="[% d %]">[% path_elements.push(d); print_output(remaining, path_elements); %]</td>
[% SET discard = path_elements.pop %]
[% ELSE %]
<td>([% path_elements.join('/')  _ '/' _ d  %])</td>
[% END %]
[% END %]
</tr></table>
[% END %]
[% SET path = []; print_output(records, path) %]

2 个答案:

答案 0 :(得分:1)

我不确定我是否了解您工作的完整背景,但这是对一般问题的抨击:

use strict;
use warnings;

my @array = (
    [ qw/00 01/ ],
    [ qw/10/ ],
    [ qw/20 21/ ],
    [ qw/30 31 32 33/ ],
);
print_output(\@array);

sub print_output {
    my ($data, @path_elements) = @_;
    my $level = @path_elements;
    my ($current, @remaining) = @$data;
    for my $d (@$current){
        print '  ' x $level, "<$d>\n";    
        if (@remaining){
            print_output(\@remaining, @path_elements, $d);        
        }
        else {
            print '  ' x ($level + 1), "(", join('/', @path_elements, $d), ")\n";
        }
        print '  ' x $level, "</$d>\n";
    }
}

答案 1 :(得分:1)

如果您需要Template Toolkit解决方案,请参阅下文。

Perl代码:

use strict;
use Template;

my @array = (
    [qw /00 01/],
    [qw /10/],
    [qw /20 21/],
    [qw /30 31 32 33/],
);

my $tt = Template->new(POST_CHOMP => 1);

$tt->process('template.tt', { DATA => \@array }) or die "TT Error : " . $tt->error();

TT模板(已修复)(template.tt):

[% BLOCK display -%]
   [% arr = DATA.$i %]
   [% IF i == DATA.max %]
      [% FOREACH t IN arr -%]
         <[% t %]>
             [% tmp_c = c.substr(1) %]
             [% "($tmp_c/$t)" %]
         </[% t %]>
      [% END %]
   [% ELSE %]
      [% FOREACH t IN arr -%]
         <[% t %]>
            [% INCLUDE display i = i+1, c = "$c/$t" %]
         </[% t %]>
      [% END -%]
   [% END %]
[% END -%]

[% INCLUDE display i = 0, c = '' %]
相关问题