I know, the following code seems to be confusing, but if you get a better understanding of
map (perldoc -f map) and
grep (perldoc -f grep), the code is not very complicated...
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
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
my $source_file = './source.txt';
tie my @array,'Tie::File',$source_file or die $!;
my $string = $array[0];
chomp $string;
my $source = substr($string,0,index($string,'|',index($string,'|')+1));
my $dest = substr($string,index($string,'|',index($string,'|')+2));
my @sources = grep{$_}split(/[\s|]+/,$source);
my @dests = grep{$_}split(/[\s|]+/,$dest);
for(1..scalar(@array)-1){
$string = $array[$_];
my $source2 = substr($string,0,index($string,'|',index($string,'|')+1));
my $dest2 = substr($string,index($string,'|',index($string,'|')+2));
my @tmp = grep{defined $_ and $_ ne ''}split(/[\s|]+/,$source2);
my @info = map{$sources[$_]}grep{$tmp[$_]}(0..scalar(@tmp)-1);
@tmp = grep{defined $_ and $_ ne ''}split(/[\s|]+/,$dest2);
my @targets = map{$dests[$_]}grep{$tmp[$_]}(0..scalar(@tmp)-1);
print join(',',@info),' --> ',join(',',@targets),";\n";
}
untie @array;
the source file:
G11 G12 G13 |G21 G22 |G31 G32 G33 |
1 0 1 | 1 0 | 1 0 1 |
0 0 1 | 0 1 | 0 1 1 |
Edit: dann hätte ich ja auch auf Deutsch antworten können ;)\n\n
<!--EDIT|renee|1149587626-->