http://www.perlmonks.org?node_id=989748


in reply to Re: A complicated file parsing and 2D array/matrix problem.
in thread A file parsing and 2D array/matrix problem.

Hi guys. I have finally managed to assemble this code with some help.
my %supermatrix; my $list = "LIST.txt"; open my $DATA, '<', $list or die "unable to open my list $list $!\n"; print "\nWORKING ON $list\n"; print `head $list`; while (my $line = <$DATA>) { chomp $line; my $substrate = 1 if $line =~ /substrate.*(\d+)$/; print "SUBSTRATE ===$substrate"; $line = <DATA>; # fetch next line print "PRODUCT====$line"; my @products; (undef, @products) = split / /, $line; foreach my $prod (@products) { $supermatrix{$substrate}{$prod} = 1; print "combination $substrate $prod exists ! \n" if exists $ +supermatrix{$substrate}{$prod}; } }

But there are two problems :-

1.] Its not printing any output.

2.] My list would at max be ~5 to 10% smaller than superarrays(whereas this code will handle output data sparsely). Also I need the output in such a format that there are blanks wherever there isnt a "1".

Actually the quality of the output depends on the number of blanks also, because this output will be then compared to other 20 such outputs. So in that sense the position of "blanks" and "1" is equally important.

Replies are listed 'Best First'.
Re^3: A complicated file parsing and 2D array/matrix problem.
by Kenosis (Priest) on Aug 26, 2012 at 00:28 UTC

    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!

      Hello Kenosis. Now there's the last piece of puzzle. I have created 5 such matrices (with obviously same number of rows and column). Now the problem is that I have to concatenate (OR logic operation) two such matrices,

      INPUT = Two matrices A,B of same row and column saved in text files A.txt and B.txt

      OUTPUT = A single matrix C ( Cij = Aij OR Bij )

      ==============INPUT======= MAT - A 1875 2809 3182 3419 2809 - 1 1 - 3182 1 - - - 3186 1 1 - - 3485 - - - - 3486 - - - - MAT - B 1875 2809 3182 3419 2809 1 - - 1 3182 - - - - 3186 - 1 1 - 3485 - - - - 3486 - 1 - 1 ========== OUTPUT=========== MAT - C 1875 2809 3182 3419 2809 1 1 1 1 3182 1 - - - 3186 1 1 1 - 3485 - - - - 3486 - 1 - 1
      I.e. an element of matrix will be one if either of the corresponding element of A or B is one.

        Hi, zing!

        What have you tried so far to solve this puzzle?

      Kenosis, Theres a small problem. I tried to check whether the code is giving desired output,but it isnt giving the complete result.Consider my

      LIST.txt

      substrate[s]: 1 2 product[s]: 3 substrate[s]: 6 9 product[s]: 8 10 substrate[s]: 3 product[s]: 6 substrate[s]: 9 product[s]: 5 substrate[s]: 5 product[s]: 2 substrate[s]: 3 product[s]: 9 substrate[s]: 8 product[s]: 9 substrate[s]: 8 product[s]: 1 substrate[s]: 7 product[s]: 11 substrate[s]: 19 product[s]: 17 substrate[s]: 14 product[s]: 13 substrate[s]: 14 product[s]: 11 substrate[s]: 18 product[s]: 19 substrate[s]: 7 14 product[s]: 15 substrate[s]: 7 16 product[s]: 7 17 substrate[s]: 5 product[s]: 6 substrate[s]: 18 15 product[s]: 7 substrate[s]: 7 8 product[s]: 8 18 substrate[s]: 6 product[s]: 9 substrate[s]: 11 product[s]: 12
      SUPERLIST_SUBSTRATE
      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
      SUPERLIST_PRODUCT
      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
      ********OUTPUT********
      p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 s1 . . 1 . . . . . . . . . . . . . . . . s2 . . . . . . . . . . . . . . . . . . . s3 . . . . . 1 . . 1 . . . . . . . . . . s4 . . . . . . . . . . . . . . . . . . . s5 . 1 . . . 1 . . . . . . . . . . . . . s6 . . . . . . . 1 1 1 . . . . . . . . . s7 . . . . . . 1 1 . . 1 . . . 1 . 1 1 . s8 1 . . . . . . . 1 . . . . . . . . . . s9 . . . . 1 . . . . . . . . . . . . . . s10 . . . . . . . . . . . . . . . . . . . s11 . . . . . . . . . . . 1 . . . . . . . s12 . . . . . . . . . . . . . . . . . . . s13 . . . . . . . . . . . . . . . . . . . s14 . . . . . . . . . . 1 . 1 . . . . . . s15 . . . . . . . . . . . . . . . . . . . s16 . . . . . . . . . . . . . . . . . . . s17 . . . . . . . . . . . . . . . . . . . s18 . . . . . . 1 . . . . . . . . . . . 1 s19 . . . . . . . . . . . . . . . . 1 . .
      Consider first two lines of LIST.txt -
      substrate[s]: 1 2 product[s]: 3
      . Then there is a "1" s1-p3, but there isn;t a "1" for s2-p3. ################DESIRED OUTPUT -- Places marked with "X" need to be "1" #################
      p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 s1 . . 1 . . . . . . . . . . . . . . . . s2 . . X . . . . . . . . . . . . . . . . s3 . . . . . 1 . . 1 . . . . . . . . . . s4 . . . . . . . . . . . . . . . . . . . s5 . 1 . . . 1 . . . . . . . . . . . . . s6 . . . . . . . 1 1 1 . . . . . . . . . s7 . . . . . . 1 1 . . 1 . . . 1 . 1 1 . s8 1 . . . . . . X 1 X . . . . . . . X . s9 . . . . 1 . . . . . . . . . . . . . . s10 . . . . . . . . . . . . . . . . . . . s11 . . . . . . . . . . . 1 . . . . . . . s12 . . . . . . . . . . . . . . . . . . . s13 . . . . . . . . . . . . . . . . . . . s14 . . . . . . . . . . 1 . 1 . X . . . . s15 . . . . . . X . . . . . . . . . . . . s16 . . . . . . X . . . . . . . . . X . . s17 . . . . . . . . . . . . . . . . . . . s18 . . . . . . 1 . . . . . . . . . . . 1 s19 . . . . . . . . . . . . . . . . 1 . .

        Hi, zing.

        Was this issue resolved? I noticed a new posting where a blank line in a file was mentioned...