
rushadrena
Novice
Sep 12, 2012, 12:16 AM
Post #60 of 62
(1546 views)
|
|
Re: [Chris Charley] A file parsing and 2D array/matrix problem.
[In reply to]
|
Can't Post
|
|
Hi Chris, I modified your code to cater to my requirements (see parts in itallics).:-
#!/usr/bin/perl use strict; use warnings; use List::Util qw/ max /; open my $fh, "<", 'SUPERLIST_PRODUCT' or die $!; my $spr_prod = do {local $/; <$fh>}; close $fh or die $!; my @spr_prod = $spr_prod =~ /\d+/g; open $fh, "<", 'SUPERLIST_SUBSTRATE' or die $!; my $spr_substr = do {local $/; <$fh>}; close $fh or die $!; my @spr_substrate = $spr_substr =~ /\d+/g; my $xx=0; my $zz=0; my $i=0; my $j=0; my @matrix; my @file; my @dirs = split /\n/,`find -maxdepth 1 -type d`; splice(@dirs, 0, 1); for ($xx=0;$xx<=9;$xx++) { for ($zz=0;$zz<=9;$zz++) { my $path = '.'; # (current directory - '.') or path to data files my @file = ("$dirs[$xx]/irrev_rev_revdup", "$dirs[$zz]/irrev_rev_revdup" ); for my $file ( @file ) { my %data; my @substrate; print "#########################$file###################################"; open my $fh, "<", "$file" or die "Unable to open $file for reading. $!"; while (<$fh>) { if (/^substrate/) { @substrate = /\d+/g; } elsif (/^product/) { while (/(\d+)/g) { for my $sub (@substrate) { $data{$sub}{$1} = 1 ; } } } else { die "Unknown format $file. $!"; } } close $fh or die "Unable to close $file. $!"; print "Processing file: $file\n"; process(\@spr_prod, \@spr_substrate, %data); push @matrix, \%data; } my %dat = combine($matrix[$i], $matrix[$j]); print "Combined matrix $file[$i] and $file[$j]" process(\@spr_prod, \@spr_substrate, %dat); }} sub process { my ($spr_prod, $spr_subst, %data) = @_; my %seen; my @product = sort {$a <=> $b} grep ! $seen{$_}++, @$spr_prod, map keys %$_, values %data; # to get column width for print my $wid = 1 + max map length, @product; printf "%7s" . "%${wid}s" x @product . "\n", 'prod->', @product; undef %seen; my @substrate = sort {$a <=> $b} grep ! $seen{$_}++, @$spr_subst, keys %data; for my $substrate (@substrate) { printf "%7s", $substrate; printf "%${wid}s", $data{$substrate}{$_} || '-' for @product; print "\n"; } printf "\n%5s\n%5s\n%s\n\n", '^', '|', 'substrate'; } sub combine { my ($matrix1, $matrix2) = @_; my %new_hash = %$matrix1; for my $substrate (keys %$matrix2) { $new_hash{$substrate}{$_} = 1 for keys %{ $matrix2->{$substrate} }; } return %new_hash; } And here's a snippet from the output :-
#########################./a/irrev_rev_revdup######################Processing file: ./a/irrev_rev_revdup prod-> 1 2 3 4 5 6 7 8 9 10 1 - - - - - - - - - 1 2 - - 1 - 1 - - 1 - - 3 - - 1 1 - - 1 1 1 - 4 - - - - - - - - - - 5 - - 1 - - - - - 1 - 6 - - - - - - 1 - - - 7 - 1 1 - - 1 - 1 - - 8 - - - - - - - - - - 9 - - 1 - - - - 1 - 1 10 1 - - - - - - - 1 - ^ | substrate #########################./b/irrev_rev_revdup#####################Processing file: ./b/irrev_rev_revdup prod-> 1 2 3 4 5 6 7 8 9 10 1 1 - 1 - - - - 1 - - 2 1 1 - - 1 - 1 - - - 3 1 1 1 1 - - 1 1 1 - 4 - - - - - - - - - - 5 1 1 1 1 - - - - 1 - 6 - 1 - - - - 1 - - - 7 - 1 - - - 1 1 - - - 8 - - - - - - - - - - 9 1 - 1 - - - - 1 - - 10 1 - - - - - - - 1 - ^ | substrate Combined matrix ./a/irrev_rev_revdup and ./a/irrev_rev_revdup prod-> 1 2 3 4 5 6 7 8 9 10 1 - - - - - - - - - 1 2 - - 1 - 1 - - 1 - - 3 - - 1 1 - - 1 1 1 - 4 - - - - - - - - - - 5 - - 1 - - - - - 1 - 6 - - - - - - 1 - - - 7 - 1 1 - - 1 - 1 - - 8 - - - - - - - - - - 9 - - 1 - - - - 1 - 1 10 1 - - - - - - - 1 - ^ | substrate #########################./a/irrev_rev_revdup####################Processing file: ./a/irrev_rev_revdup prod-> 1 2 3 4 5 6 7 8 9 10 1 - - - - - - - - - 1 2 - - 1 - 1 - - 1 - - 3 - - 1 1 - - 1 1 1 - 4 - - - - - - - - - - 5 - - 1 - - - - - 1 - 6 - - - - - - 1 - - - 7 - 1 1 - - 1 - 1 - - 8 - - - - - - - - - - 9 - - 1 - - - - 1 - 1 10 1 - - - - - - - 1 - ^ | substrate #########################./c/irrev_rev_revdup###################Processing file: ./c/irrev_rev_revdup prod-> 1 2 3 4 5 6 7 8 9 10 1 - - - - - - - - - 1 2 - - 1 - 1 - - 1 - - 3 1 - 1 1 1 - 1 1 1 - 4 - - - - - - - - - - 5 1 - 1 - 1 - 1 1 1 - 6 - - - - - - 1 - - - 7 1 1 1 - 1 1 1 1 - - 8 - - - - - - - - - - 9 - - 1 - - - - 1 - 1 10 1 1 - 1 1 - - - 1 - ^ | substrate Combined matrix ./a/irrev_rev_revdup and ./a/irrev_rev_revdup prod-> 1 2 3 4 5 6 7 8 9 10 1 - - - - - - - - - 1 2 - - 1 - 1 - - 1 - - 3 - - 1 1 - - 1 1 1 - 4 - - - - - - - - - - 5 - - 1 - - - - - 1 - 6 - - - - - - 1 - - - 7 - 1 1 - - 1 - 1 - - 8 - - - - - - - - - - 9 - - 1 - - - - 1 - 1 10 1 - - - - - - - 1 - As you can see the concatenated matrix isn't correct in any of the cases. I hope you are getting me, the output should have been like this:- Processing file: ./a/irrev_rev_revdup Processing file: ./b/irrev_rev_revdup Combined matrix ./a/irrev_rev_revdup and ./b/irrev_rev_revdup (Along with correct concatenation)
|