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
| use strict;
use warnings;
use feature qw(:5.10);
sub load_table {
my ($result_tabref, $result_keyref, $file) = @_;
open my $TABLE, "<", $file or die "Can't open $file: $!";
chomp(my $line = <$TABLE>);
my ($test_key, @col_names) = split /\s+/, $line;
if (!@$result_tabref) {
# create the first row
push @$result_tabref, [ $test_key, @col_names ];
}
else {
# complete the first row
push @{$result_tabref->[0]}, @col_names;
}
my $col_number = @{$result_tabref->[0]};
while (defined($line = <$TABLE>)) {
chomp $line;
my ($test_key, @col_names) = split /\s+/, $line;
if (exists $result_keyref->{$test_key}) {
# complete an existing row
my $row_index = $result_keyref->{$test_key};
push @{$result_tabref->[$row_index]}, @col_names;
}
else {
# create a list of missing column values (from the column number of the first row)
my @missing_col = map "--", 1 .. ($col_number - 1 - @col_names);
push @$result_tabref, [ $test_key, @missing_col, @col_names ];
$result_keyref->{$test_key} = $#$result_tabref;
}
}
# treat incomplete rows
foreach my $row (grep @$_ != $col_number, @$result_tabref) {
push @$row, map "--", 1 .. ($col_number - @$row);
}
}
sub print_table {
my ($tabref, $file) = @_;
open my $OUT, ">", $file or die "Can't open $file: $!";
say { $OUT } join " ", @$_ foreach @$tabref;
}
my (@table, %test_keys);
load_table(\@table, \%test_keys, "fic1.txt");
load_table(\@table, \%test_keys, "fic2.txt");
print_table(\@table, "fic3.txt"); |
Partager