I wanted to remove white spaces from multiple datafiles through the for loop below. I inserted the for loop inside a function. The function can read the input data files, but the output files print doesn't work correctly unless I reset the variable ($new_data) after every time it prints to a file. Otherwise, the earlier data gets appended to the later data. Also, is there any problem if the same files as the input and output since I have no use of the input file later?
Pass @row for reading from inputFile and $new_data for writing to the outputFile
$dir = '/****/';
$inputFileSpring = $dir . "SpringSIMS.dat";
$inputFileSummer = $dir . "SummerSIMS.dat";
$inputFileFall = $dir . "FallSIMS.dat";
$outputFileSpring = $dir . "Spring.dat";
$outputFileSummer = $dir . "Summer.dat";
$outputFileFall = $dir . "Fall.dat";
#Read Spring SIMS Data
open (NOTE, "$inputFileSpring" || die "Could not open $inputFileSpring\n");
processFile(@row=<NOTE>);
close(NOTE);
#Write Spring Data
open(NOTE, ">$outputFileSpring" || die "Could not open $inputFileSpring\n");
print NOTE $new_data;
close(NOTE);
reset('new_data');
#Read Summer SIMS Data
open (NOTE, "$inputFileSummer" || die "Could not open $inputFileSummer\n");
processFile(@row=<NOTE>);
close(NOTE);
#Write Summer Data
open(NOTE, ">$outputFileSummer" || die "Could not open $inputFileSummer\n");
print NOTE $new_data;
close(NOTE);
reset('new_data');
#Read Fall SIMS Data
open (NOTE, "$inputFileFall" || die "Could not open $inputFileFall\n");
processFile(@row=<NOTE>);
close(NOTE);
#Write Fall Data
open(NOTE, ">$outputFileFall" || die "Could not open $inputFileFall\n");
print NOTE $new_data;
close(NOTE);
reset('new_data');
sub processFile
{
for $row(@row) {
chop($row);
@field = split(/\|/, $row);
for ($i=0; $i<@field; $i++) {
if ($field[$i] =~ /^ /)
{
$field[$i] = " ";
}
else
{
$field[$i] =~ s/ *$//g;
}
$new_data .= $field[$i] . "|";
}
$lastchar = chop($new_data);
if (@field == 15) {
$new_data .= "|0";
}
$new_data .= "\n";
}
# return $new_data;
} # END sub processFile
exit;