Hi,

I have a simple code that take
currently fixed to take 3 input files and
append each line of these files together.

I wish to know is there anyway I can
write the code more flexibly so it can
dynamically take the input argument - from minimum
2 files to more.

Is there anywhere I can find information
about dealing with this matter? Any specific
module that cater this problem?

Thanks for your time beforehand.

Regards,
Edward WIJAYA
SINGAPORE


This is my current (terribly inefficient and unflexible) code:

#!/usr/bin/perl -w
use strict;
use Data::Dumper;


if(scalar @ARGV ne 1) { print "Usage: $0 file1 file2 [file3] etc\n"; exit(1); } #the above part doesn't seem to be working well/useful

my $file1 = $ARGV[0];
my @set1 = get_file_data("$file1");

#Remove the whitspace and \n from each lines
@set1 = grep { s/^\s+//; s/\s+$//; s/\s+/ /g; length } @set1;
my $s1 = [EMAIL PROTECTED];

my $file2 = $ARGV[1];
my @set2     = get_file_data("$file2");
@set2 = grep { s/^\s+//; s/\s+$//; s/\s+/ /g; length } @set2;
my $s2 = [EMAIL PROTECTED];

my $file3 = $ARGV[2];
my @set3     = get_file_data("$file3");
@set3 = grep { s/^\s+//; s/\s+$//; s/\s+/ /g; length } @set3;
my $s3 = [EMAIL PROTECTED];



#Printout the appended lines from the data
print map { join("",$set1[$_],$set2[$_],$set3[$_]), "\n" } (0..$#set1);



#Subroutine
sub get_file_data {

    my ($filename) = @_;

    use strict;
    use warnings;

    # Initialize variables
    my @filedata = ();

    unless ( open( GET_FILE_DATA, $filename ) ) {
        print STDERR "Cannot open file \"$filename\"\n\n";
        exit;
    }

    @filedata = <GET_FILE_DATA>;
    close GET_FILE_DATA;
    return (@filedata);
}


___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