while(read $fh, my $buffer, 128*2**10) {
$buffer .= <$fh>; # since last line probably crosses buffer border
for(split /\n/, $buffer) {
# ...
}
}
####
my @field = split /\|/, $_, 10; # each record has 10 fields
####
# we assume 10 fields per record again
while(read $fh, my $buffer, 128*2**10) {
$buffer .= <$fh>;
# we dump all records' fields in a big pile, in which
# every 9th element contains the last field of one record,
# plus a newline, plus the first field of the next record
my @in_field_heap = split /\|/, $buffer;
while(@in_field_heap) {
# pull the two glued fields apart
$in_field_heap[9] =~ /^([^\n]*)\n(.*)/;
# pull out fields of current record incl the glued one,
# and reinject the second half of the double field
my @field = splice @in_field_heap, 0, 10, $2;
# replace the glued double field by its first half
@field[9] = $1;
# ...
}
}
####
# ...
push @out_field_heap, @field, "\n";
}
# single join over the whole batch
my $out_buffer = join "|", @out_field_heap;
# but that means we surrounded the newlines with pipes,
# so fix em
$out_buffer =~ s/\|\n\|/\n/g;
print OUTPUT $out_buffer;
}
####
# 10 fields..
while(read $fh, my $buffer, 128*2**10) {
$buffer .= <$fh>;
$buffer =~ s/\n/\|/g;
my @in_field_heap = split /\|/, $buffer;
while(my @field = splice @in_field_heap, 0, 10) {
# ...
}
}