R
Richard Kofler
[ sorry for my bad English ]
I try to process the ouput of a database utility,
see the format after __DATA__ below.
Test data shows the structure hopefully clear enough.
Reading perldoc about the .. and ... operators, and googling
for examples in perl.beginners and here I was not
able to accomplish using those, what I was able to put
together in a 'not-so-perlish' way.
The script I have now, does what I need, but I want to
ask if there is a better way of processing lines of text
which are separated by a special type of line,
detectable with means of a regexp, much like a headline,
at least sort of but not like a paragraph type headline.
Here is my script:
--------------------------------
#!/usr/bin/perl
use warnings;
use strict;
sub print_linebuf {
my $rbuf = $_[0];
my $linecnt = 0;
# print all but the last line from buffer
foreach my $line ( @{ $rbuf } ) {
$linecnt++;
print $line, "\n" unless ($linecnt == scalar(@{ $rbuf }));
}
@{ $rbuf } = ( );
}
my @linebuf;
my $seen = 1;
my $tag;
while (<DATA>) {
chomp;
if (/-+\s+(\d+)\s+-+/) {
# save the block identifier (SID)
$tag = $1;
$seen = 0;
print_linebuf(\@linebuf) unless ($seen);
}
my $taggedstr = $tag . " " . $_;
push @linebuf, $taggedstr;
}
print_linebuf(\@linebuf);
__DATA__
----- 501 -----
A
B
C
----- 202 -----
1
2
----- 03 -----
D
E
F
----- 04 -----
H
I
J
----- 754 -----
4
5
----- 22612 -----
6
7
8
9
I try to process the ouput of a database utility,
see the format after __DATA__ below.
Test data shows the structure hopefully clear enough.
Reading perldoc about the .. and ... operators, and googling
for examples in perl.beginners and here I was not
able to accomplish using those, what I was able to put
together in a 'not-so-perlish' way.
The script I have now, does what I need, but I want to
ask if there is a better way of processing lines of text
which are separated by a special type of line,
detectable with means of a regexp, much like a headline,
at least sort of but not like a paragraph type headline.
Here is my script:
--------------------------------
#!/usr/bin/perl
use warnings;
use strict;
sub print_linebuf {
my $rbuf = $_[0];
my $linecnt = 0;
# print all but the last line from buffer
foreach my $line ( @{ $rbuf } ) {
$linecnt++;
print $line, "\n" unless ($linecnt == scalar(@{ $rbuf }));
}
@{ $rbuf } = ( );
}
my @linebuf;
my $seen = 1;
my $tag;
while (<DATA>) {
chomp;
if (/-+\s+(\d+)\s+-+/) {
# save the block identifier (SID)
$tag = $1;
$seen = 0;
print_linebuf(\@linebuf) unless ($seen);
}
my $taggedstr = $tag . " " . $_;
push @linebuf, $taggedstr;
}
print_linebuf(\@linebuf);
__DATA__
----- 501 -----
A
B
C
----- 202 -----
1
2
----- 03 -----
D
E
F
----- 04 -----
H
I
J
----- 754 -----
4
5
----- 22612 -----
6
7
8
9