Edward Wijaya wrote:
How can I make my code above such that it can take all multiple files iteratively?

When possible try to use the data and throw it away as soon as possible to avoid high memory usage. For example, your version reads all of both files in to arrays, then creates another array containing the sums. If the individual contents of the file aren't needed beyond summing them, you can do something like below as save the memory.


use strict;
use warnings;

while ( my $basename = shift( @ARGV ) ) {
    open( my $fa, '<', "${basename}.fa" ) or die $!;
    open( my $fb, '<', "${basename}.rs" ) or do {
        close( $fa );
        die $!;
    };

    while ( !eof( $fa ) && !eof( $fb ) ) {
        chomp( my $la = <$fa> );
        chomp( my $lb = <$fb> );
        print "$la + $lb = ", $la + $lb, "\n";
    }

    if ( eof( $fa ) && eof( $fb ) ) {
        print "exiting normally.\n";
    } elsif ( eof( $fa ) ) {
        print "more to read from b\n";
    } elsif ( eof( $fb ) ) {
        print "more to read from a\n";
    } else {
        print "this can't happen\n";
    }

    close( $fa );
    close( $fb );
}


__END__


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