use Modern::Perl;
use File::Slurp qw/read_file/;
use Text::Table;
use Data::Dumper;
my ( %supermatrix, @titles, %seen, @rows );
my @list = read_file 'LIST.txt';
for ( my $i = 0 ; $i < $#list + 1 ; $i += 2 ) {
my ($substrateID) = $list[$i] =~ /(\d+)/g;
$supermatrix{$substrateID}{$1} = 1 while $list[ $i + 1 ] =~ /(\d+)
+/g;
}
for my $product ( read_file 'SUPERLIST_PRODUCT.txt' ) {
my ($productID) = $product =~ /(\d+)/g;
push @titles, $productID unless $seen{$productID}++;
for my $substrate ( read_file 'SUPERLIST_SUBSTRATE.txt' ) {
my ($substrateID) = $substrate =~ /(\d+)/g;
$supermatrix{$substrateID}{$productID} //= '.';
}
}
my $titles = join ',',
map "{title => 'p$_', align_title => 'center', align => 'center'}",
sort { $a <=> $b } @titles;
for my $y ( sort { $a <=> $b } keys %supermatrix ) { #rows
my ( $rowLable, @row );
for my $x ( sort { $a <=> $b } keys %{ $supermatrix{$y} } ) { #
+columns
$rowLable = $y unless $rowLable;
push @row, $supermatrix{$y}{$x};
}
push @rows, [ "s$rowLable", @row ];
}
my $tb = Text::Table->new( ' ', eval $titles );
$tb->load(@rows);
say $tb;
say "\n", Dumper \%supermatrix;
Partial output:
p1825 p1875 p2543 p2809 p3182 p3186 p3419 p3485 p3486 p3487 p348
+8 p3489 p3490 p3492 p3647 p3648 p3674 p3877
s2809 . . . . 1 . . . . . .
+ . . . . . . .
s3006 . . . . . . . . . . .
+ . . . . . . .
s3049 . . . . . . . . . . .
+ . . . . . . .
s3182 . 1 . . . . . . . . .
+ . . . . . . .
s3186 . . . 1 . . . . . . .
+ . . . . . . .
s3314 . . . . . . . . . . .
+ . . . . . . .
s3485 . . . . . . . . 1 . .
+ . . . . . . .
s3486 . . . . . . . . . . 1
+ . . . . . . .
s3487 . . . . . . . . . . .
+ . . . . . . 1
s3488 . . . . . . . . . 1 .
+ . . . . . . .
s3489 . . . . . . . . . . .
+ . 1 . . . . .
s3490 . . . . . . . 1 . . .
+ . . . . . . .
s3492 . . . . . . . . . . .
+ . . . . . . .
s3645 . . . . . . . . . . .
+ . . . 1 . . .
s3649 . . . . . . 1 . . . .
+ . . . . 1 . .
s3659 . . . . . . . . . . .
+ . . . 1 . . .
s3674 . . . . . . . . . . .
+ 1 1 . . . . .
s3675 . . . . . . . . . . .
+ . . . . . 1 .
s3877 . . . . . . 1 . . . .
+ . . . . . . .
$VAR1 = {
'3182' => {
'1825' => '.',
'3182' => '.',
'3877' => '.',
'3647' => '.',
'3489' => '.',
'3419' => '.',
'2809' => '.',
'3488' => '.',
'1875' => 1,
'2543' => '.',
'3492' => '.',
'3485' => '.',
'3186' => '.',
'3487' => '.',
'3648' => '.',
'3674' => '.',
'3490' => '.',
'3486' => '.'
},
...
Values are stored in the hash as follows: $supermatrix{substrateID}{productID}, where substrateIDs name the rows, and productIDs name the columns.
The hash is first initialized using the data from LIST.txt, assigning 1 to each substrateID/productID 'location.' The next, nested for loops complete the matrix, assigning a '.' to undefined substrateID/productID 'locations' (the '.' was used so the matrix could be visualized).
Finally, the matrix is printed, followed by a dump of the hash.
Hope this helps! |