The following code duplicates your new problem. I am using a memory file to simulate a disk file because I can show exactly what is on it. I am redirecting the output to STDOUT so we can see it. There is only one long line of output because you 'removed' the newline along with the comma.
use strict;
use warnings;
use autodie;
my $simulation = \do{
'field 01' . ','x79 . 'field 80,' . "\N{LF}"
.'field 01' . ','x79 . 'field 80,' . "\N{LF}"
};
*OUT = *STDOUT;
my $OFFSET = -2; # second last chracter (the ',') in $line
my $LENGTH = 2; # replace 2 charactrers (',' and \n)
my $REPLACEMENT = ''; # replace specified characters with nothing
open IN, '<', $simulation;
while(<IN>) {
my $line = $_;
my $number = () = $line =~ /\,/gi;
substr($line, $OFFSET, $LENGTH) = '' if ($number > 79);
print OUT "$line";
}
close OUT;
close IN;
In this case, you should only delete one character, the last comma. (Change $LENGTH to 1) Why do you think you should delete two characters? Is my simulation wrong? How? It does assume that you are on a *NIX system. On other systems, perl's IO Layers hide the difference. Your loop should be identical.
|