On 25/04/2012 13:52, Paul Clark wrote:
Hi All,
I have a small project that is stumping me aside from using a straight brute
force.
I am processing output from an accounting program that is producing some
sort of printer control for some 3rd party print processing. I have
several partial lines that have commands to "over write" the line to create
bold type. I need combine the lines:
1 Balance Due:
0 Balance Due: $567.23
0 $567.23 Before Due Date:
0 Before Due Date: 06/15/12
0 06/15/12
So the output line should be:
Balance Due: $567.23 Before Due Date: 06/15/12
The problem is the lines can be variable so I cannot just use substr to copy
parts of lines. The brute force was I was going to use is to just create
an output array for the line and loop through each line position by position
and if the character was not a space, set that position in the output array
to the character in the input line.
Any suggestions for a more elegant solution?
Hello Paul
I don't know if you saw my previous answer to this question, but here it
is again.
I imagine the output lines aren't as shown in your mail? I presume the
text to be overwritten appears in the same character colums as in the
preceding lines? Something more like
1 Balance Due:
0 Balance Due: $567.23
0 $567.23 Before Due Date:
0 Before Due
Date: 06/15/12
0
06/15/12
I would use substr together with the predefined Perl arrays @- and @+ to
overwrite a line buffer with the non-space substrings within each line.
Read about the arrays at
http://perldoc.perl.org/perlvar.html#Variables-related-to-regular-expressions
Note that the optional fourth parameter of substr replaces the specified
substring.
This piece of code shows the idea.
HTH,
Rob
use strict;
use warnings;
my $line;
while (<DATA>) {
s/\s+$//;
my $short = length($_) - length($line);
$line .= ' ' x $short if $short > 0;
while (/(\S+)/g) {
my $beg = $-[1];
my $len = $+[1] - $-[1];
substr $line, $beg, $len, $1;
}
}
print $line;
__DATA__
1 Balance Due:
0 Balance Due: $567.23
0 $567.23 Before Due Date:
0 Before Due
Date: 06/15/12
0
06/15/12
**OUTPUT**
0 Balance Due: $567.23 Before Due
Date: 06/15/12
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/