can I use 3 dot operator?

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
 
B

Brian McCauley

Richard said:
Subject: can I use 3 dot operator?

Probably not usefully in this case.
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 } = ( );
}

This is more simply

sub print_linebuf {
my $rbuf = shift;
pop @$rbuf;
print "$_\n" for @$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);

The variable $seen in the above code serves no purpose.

my @linebuf;
my $tag;

while (<DATA>) {
chomp;

if (/-+\s+(\d+)\s+-+/) {
# save the block identifier (SID)
$tag = $1;
print_linebuf(\@linebuf);
}
push @linebuf, "$tag $_";
}

print_linebuf(\@linebuf);
 
R

Richard Kofler

Brian said:
Probably not usefully in this case.

[ ... snipping useful code corrections ... ]

Thank you, Brian.
The script looks much better the way, you
showed me to do it!

dic_k
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,884
Members
47,419
Latest member
ArturoBres

Latest Threads

Top