On Sat, 11 Sep 2004 02:20:32 +0800, Bee <[EMAIL PROTECTED]> wrote:
>
>
> > >foreach( @ARGV ) {
> > > open IN, $_ or die "Couldn't open $_: $!\n";
> > > chomp( my @data = <IN> );
> > > close IN;
> > > foreach( @data ) { s/\s+//g; }
> > > foreach( 0..$#data ) { $results[$_] .= $data[$_]; }
> > >}
> > This is a little shorter and saves on iterations:
> > for my $file (@ARGV) {
> > open IN, $file or die "Couldn't open $file: $!\n";
> > @results = map { my $line = $_; chomp $line; $line =~ s/\s+//g;
>
> @results seems would be re-assigned when the next file comes...
>
> Besides, I guess chomp is not nesessary here.. \s+ means [\t\r\n\f]+,
> so \r, \n or \r\n would seem to be cleared by the s expression.
>
> > $line } (<IN>);
> > close IN;
> > }
> >
>
> I would suggest to write in this way :
> my @res = ();
> foreach( @ARGV ) {
> open IN, $_ or die "Couldn't open $_: $!\n";
> my @data = <IN>;
> close IN;
>
> s/\s+//g for @data;
> @res = ( @res, @data );
> }
>
> Code not been tested, but the concept is something like this.
> HTH
>
>
Ok ... Thanks for helpin' out, HTH! I tried out your suggestions and
now the code looks like this:
#!/usr/bin/perl
use warnings;
use strict;
unless( @ARGV >= 2 ) { die "Usage: $0 file1 file2 [file3...]\n"; }
my @results = ();
foreach my $file ( @ARGV ) {
open IN, $file or die "Couldn't open $file: $!\n";
my @data = <IN>;
close IN;
s/\s+//g foreach( @data );
@results = ( @results, @data );
}
foreach( @results ) { print "$_\n"; }
But now the output is as follows. We want the output to look like this:
OneFirst
TwoSecond
ThreeThird
But instead, it's coming out like this:
One
Two
Three
First
Second
Third
Given the two test input files:
file1: file2:
One First
Two Second
Three Third
--Errin
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>