CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Chris Charley] A file parsing and 2D array/matrix problem.: Edit Log



rushadrena
Novice

Sep 7, 2012, 1:05 AM


Views: 5650
Re: [Chris Charley] A file parsing and 2D array/matrix problem.

Hi Chris,
I have 10 directories and each of them has a file "list".
I modified your code to create pairwise concatenated matrices but Im getting this error --"No such file or directory at code2.pl line 6", whereas the file does exist.
Here's the code :-

Code
#!/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 $i=0;
my $j=0;
my @matrix;
my @file;
my @dirs = split /\n/,`find -maxdepth 1 -type d`;
for ($i=1;$i<=10;$i++)
{
for ($j=1;$j<=10;$j++)

{

my $path = '.'; # (current directory - '.') or path to data files
my @file = qw($dirs[$i]/list $dirs[$j]/list);

for my $file ( @file ) {
my %data;
my @substrate;

open my $fh, "<", "$path/$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;
}
}}
for my $i (0 .. $#matrix) {
for my $j ($i+1 .. $#matrix) {
print "Combining $file[$i] and $file[$j]\n";
my %data = combine($matrix[$i], $matrix[$j]);
process(\@spr_prod, \@spr_substrate, %data);
}
}

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;
}



(This post was edited by rushadrena on Sep 8, 2012, 3:24 AM)


Edit Log:
Post edited by rushadrena (Novice) on Sep 8, 2012, 3:24 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives