Edward Wijaya wrote:
Hi,

Hello,

Suppose I have a pair-series of files as follows:

data1.fa
data1.rs
data2.fa
data2.rs #say each of this file contain lines of numbers
....      #and there are 40 of files

And I have a code that take two files that ends with *.fa and *.rs

$ perl mycode.pl data1.fa data1.rs
$ perl mycode.pl data2.fa data2.rs

So the code is like this:

__BEGIN__
use warnings;
use strict;

my $file_fa = $ARGV[0];
my $file_rs = $ARGV[1];
open FILE_FA, "< $file_fa" or die "Can't open $file_r : $!";
open FILE_RS, "< $file_rs" or die "Can't open $file_p : $!";

my @fa_data; #these two are of the same size
my @rs_data;


while (<FILE_FA>){ chomp; push @fa_data, $_; }

while (<FILE_RS>){
  chomp;
  push @rs_data, $_;
}

my @sum = map {$rdata[$_] + $pdata[$_]} 0..$#fa_data;

my ($base) = split /\./,$file_fa;
print "$base\n";
print ">\n";

foreach my $sum (@sum){

 print "$sum\n";
}

__END__

How can I make my code above such that it can take all multiple files iteratively? to give output sth like this:

data1

sum_of_elements from data1.fa and data1.rs

data2

sum_of_elements from data2.fa and data2.rs

Here is one way to do it:

my %files;
for ( @ARGV ) {
    next unless /(.+)\.(?:fa|rs)$/;
    push @{ $files{ $1 } }, $_;
    }

for my $base ( keys %files ) {
    if ( @{ $files{ $base } } != 2 ) {
        warn "Error: only one file '@{$files{$base}}'.\n";
        next;
        }
    die "Cannot open files '@{$files{$base}}'.\n"
        unless open F, '<', $files{ $base }[ 0 ]
           and open G, '<', $files{ $base }[ 1 ];
    print "$base\n>\n";
    while ( my $f = <F> and my $g = <G> ) {
        print $f + $g, "\n";
        }
    }

__END__



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to