What are you doing there?
foreach $line (@array)
{
$line =~ s/\s{1}//g;
}
You are aliasing $line to the elements of @array; but because you emptied it just before, the loop never executes at all. Even if it did, you would be substituting on the array's elements; but what you want is to do so on the line of input data.
I'll quote from perldoc -f split:
As a special case, specifying a PATTERN of space (' ') will split on white space just as split with no arguments does. Thus, split(' ') can be used to emulate awk's default behavior, whereas split(/ /) will give you as many null initial fields as there are leading spaces. A split on /\s+/ is like a split(' ') except that any leading whitespace produces a null first field.
In other words,
just @array = split (" ", $line);
and you should be fine.
(And it rhymes, too.)
I'd like to mention you should rather move the my @array; into the loop - instead of emptying the array every time over. Variables should be declared as close to where they're used as possible, and restricted to as narrow a scope as feasible.
Taking all this together and using the fact that you can perfectly well work with $_ without assigning it to something first, we get this:
#!/usr/local/bin/perl -w
use strict;
open (FH, $ARGV[0]) or die "unable to open input file: $!"; # more hel
+pful message
open (OUTFILE, ">$ARGV[1]") or die "unable to open output file: $!"; #
+ this should have one too
while (<FH>){
chomp;
my @array = split;
print OUTFILE "@array\n"; # or whatever has to be done
}
As a general coding style, I would propose you don't try to handle the filenames yourself; let Perl decide about the input arguments and let the user decide, via redirection, where he wants to put the stuff. So we get this:
#!/usr/local/bin/perl -w
use strict;
while (<>){
chomp;
my @array = split;
print "@array\n"; # or whatever has to be done
}
A sidenote for the curious: perl -na will build almost exactly that loop framework for you. :)
Makeshifts last the longest. |