Thread rekursive subroutine
(11 answers)
Opened by kimmy at 2014-02-06 14:52 Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %data; my %hits; my %used; while(<DATA>) { next unless /^\s*\[([^\[\]]+)\]:\s*(.+?)\s*$/s; my $name=$1; my @param=split(/\s*,\s*/,$2); for(@param) { if(/\[([^\[\]]+)\]/) { push(@{$data{$name}},{type => 'node', value => '', name => $1}); push(@{$hits{$name}},$1); $used{$1}++; } else { push(@{$data{$name}},{type => 'value', value => $_, name => ''}); } } } print Dumper(\%hits); printout(\%data); my $do=1; while($do) { $do=0; for my $k (sort keys(%hits)) { my $v=$hits{$k}; my $prnt=0; for my $p (reverse 0..$#$v) { next if(exists($hits{$v->[$p]})); next if($used{$v->[$p]} > 1); my $kk=splice(@$v,$p,1); my $del=delete($data{$kk}); delete($hits{$k}) unless(@$v); for my $pp (reverse 0..$#{$data{$k}}) { if($data{$k}->[$pp]->{name} eq $kk) { splice(@{$data{$k}},$pp,1,@$del); } } $do++; $prnt++; } if($prnt) { print "---------------------\n"; printout(\%data); } } } print "---------------------\n"; printout(\%data); sub printout { my ($data)=@_; for my $key (sort keys(%$data)) { my @vals; for(@{$data->{$key}}) { if($_->{type} eq 'node') { push(@vals,"[$_->{name}]"); } else { push(@vals,$_->{value}) } } print "[$key]:".join(", ",@vals)."\n"; } } __DATA__ [1]: a, b [2]: c, [1] [3]: d, e [4]: f, [3] [5]: g, h [6]: i, [5] [7]: [2], [4] [8]: j, [6] [9]: [7], [8] Code: (dl
)
1 [1]:a, b Last edited: 2014-02-06 18:10:50 +0100 (CET) |