in reply to Re^2: A complicated file parsing and 2D array/matrix problem.
in thread A file parsing and 2D array/matrix problem.
I think you've done well in your attempt to create a matrix to represent your data set. Perhaps the following will help further your efforts:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: A complicated file parsing and 2D array/matrix problem.
by zing (Beadle) on Aug 27, 2012 at 08:12 UTC | |
by Kenosis (Priest) on Aug 27, 2012 at 18:18 UTC | |
by zing (Beadle) on Aug 27, 2012 at 19:20 UTC | |
by Kenosis (Priest) on Aug 27, 2012 at 20:15 UTC | |
Re^4: A complicated file parsing and 2D array/matrix problem.
by zing (Beadle) on Sep 04, 2012 at 07:36 UTC | |
by Kenosis (Priest) on Sep 05, 2012 at 14:59 UTC | |
by zing (Beadle) on Sep 05, 2012 at 23:17 UTC | |
by Kenosis (Priest) on Sep 06, 2012 at 01:31 UTC |