1 2 3 4 5 6 7 8
my (@data_array, @begriff_array, %begriff_hash); while(<>){ chomp; my $begriff = $1, my $matrix = $2 if (/^([^\t]+)\t(.*)/); push @begriff_array, $begriff; # Push Array of Array push @data_array, [split(/\t/,$matrix)]; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
my (%hash, %hoa); my (@data_array,@begriff_array); while(<>){ chomp; my $bez1=$1, my $bez2=$2, my $dis=$3 if(/^(.+)\t(.*)\t(.*)$/); push @{$hash{$bez1}}, $dis; next if defined $hoa{$bez1}; push @begriff_array, $bez1; $hoa{$bez1}=1; } foreach my $key (sort keys %hoa){ push @data_array, [@{$hash{$key}}]; }
QuoteNOTE: The behaviour of a "my" statement modified with a statement
modifier conditional or loop construct (e.g. "my $x if ...") is
undefined. The value of the "my" variable may be "undef", any
previously assigned value, or possibly anything else. Don't rely on
it. Future versions of perl might do something different from the
version of perl you try it out on. Here be dragons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
use warnings; my @data; sub numeric_idx { my $val = shift; return ord($val) - ord('A'); } while (<DATA>) { chomp; my ($x, $y, $v) = split /\t/; $data[numeric_idx($x)][numeric_idx($y)] = $v if defined $v; } __DATA__ A A B A 1 C A 3 C B 2 D A 2 D B 4 D C 3
1 2 3
use Dumpvalue; my $dumper = new Dumpvalue; $dumper->dumpValue(\@data_array);
1
2
3
4
5
6
7
8
9
10
11
0 ARRAY(0x100838548)
empty array
1 ARRAY(0x100838be8)
0 1
2 ARRAY(0x10083ec00)
0 3
1 2
3 ARRAY(0x10083eb58)
0 2
1 4
2 3
1
2
3
4
5
6
7
8
9
10
11
0 ARRAY(0x1008703f0)
0 ''
1 ARRAY(0x100870420)
0 1
2 ARRAY(0x100870468)
0 3
1 2
3 ARRAY(0x1008704c8)
0 2
1 4
2 3
if ($dis) {push @{$hash{$bez1}}, $dis}
push @data_array, ($hash{$key} || []);