- 论坛徽章:
- 7
|
本帖最后由 rubyish 于 2013-04-26 13:58 编辑
只是一个红薯 发表于 2013-04-26 07:36 ![]()
如果能加少许注释,我等更受益匪浅了,多谢 回 ...
少许注释:- sub parse {
- my @data = map /(\S.*\S)/, split /(if|end)/, shift;
- # @data: [ if, a then, if, b then, if, c then d, end, if, e then f, end, end, end ]
- my ( %i, %result );
- $data[$_] =~ /(if|end)/ and push @{$i{$1}}, $_ for 0 .. $#data;
- # %i: { if => [ 0, 2, 4, 7 ], end => [ 6, 9, 10, 11 ] }
- @{$i{if}} = reverse @{$i{if}};
- # reverse $i{if}:
- # %i: { if => [ 7, 4, 2, 0 ], end => [ 6, 9, 10, 11 ] }
- my @L = reverse 0 .. $#{$i{end}};
- # @L: [ 3, 2, 1, 0 ]
- for my $i ( 0 .. $#{$i{end}} ) {
- if ( $i{if}[$i] > $i{end}[$i] ) {
- # 7 > 6
- my $l;
- $i{if}[++$l] < $i{end}[$i] and last for @{$i{if}};
- # find first $i{if}[$l] < $i{end}[$i] 4 < 6; 4:index = $l = 1;
- @{ $i{if} }[ $i .. $l ] = reverse @{$i{if}}[$i .. $l];
- # %i: { if => [ 4, 7, 2, 0 ], end => [ 6, 9, 10, 11 ] }
- @L[$i .. $l] = ($L[$l]) x ($l - $i + 1);
- # @L: [ 2, 2, 1, 0 ]
- }
- my $join = "@data[$i{if}[$i] .. $i{end}[$i]]";
- # 0 .. $#{$i{end}} => 0 .. 3
- # join: if=> 4, end=> 6 = if c then d end
- # join: if=> 7, end=> 9 = if e then f end
- # join: if=> 2, end=> 10 = if b then if c then d end if e then f end end
- # join: if=> 0, end=> 11 = if a then if b then if c then d end if e then f end end end
- push @{$result{$L[$i]}}, $join;
- }
- \%result;
- }
复制代码 |
|