Well, I'm kinda new at this stuff myself, but Here's a couple of
things I see right away:


On Sat, 11 Sep 2004 10:27:44 +0800, Edward WIJAYA
<[EMAIL PROTECTED]> wrote:
> Hi,

<<SNIP>>

> #!/usr/bin/perl -w
  --You don't really need to use the -w flag here AND the 'use
warnings' in your subroutine
     down below.

> use strict;
    -- Nice Job!!

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

  --Try this:
      unless( @ARGV >= 2 ) { die "Usage: $0 file1 file2 [file3...]\n"; }

> my $file1 = $ARGV[0];
    --This is easier and prettier with this:
       my $file1 = shift;

> 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;
    --You can get all the "\n"'s out at once with a chomp:
        chomp @set1;

      However, the whole get_file_data thing and chomping can all be
done like this:
        open INFILE, $filename or die "Couldn't open $filename: $!\n";
        chomp( my @filedata = <INFILE> );

      Then, to clear out the whitespace from each line:
        foreach( @filedata ) {
          s/\s+//g;
        }

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

<<SNIP>>

Not much, I know, but I'm sure some of the others on the list will
help out more.  I'll work on this some more and post what I figure out
later.

--Errin

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