my @rec;
while (<$fh>) {
push @rec, $_;
if (/^Metadata:/) {
do_something_with_record(\@rec);
@rec = ();
}
}
| [reply] [d/l] |
Maybe just setting your "record separator" ($/) to Metadata: 1114890 will give you what you want? Otherwise, you will need to show a bit of code and a bit of representative data. Potentially two nested loops might also help, one outer loop to process the records and one inner loop to collect lines up to the next /Metadata/.
| [reply] [d/l] [select] |
A couple of possibilities come to mind:
- Since you know the record length, you could use an external counter and read them in, appending them to the end of the last record read:
my $count = 0;
my @records = ();
while (<IN>) {
if ($count) {
$records[-1] .= $_;
} else {
push @records, $_;
}
$count = ++$count % 6;
}
print join("\n",@records);
- You could slurp the file and split on a pattern that matches the last line:
my $content = do {
local $/;
<IN>;
};
my @records = split /(?<=Metadata\: 1114890\n)/, $content;
As well, I'm sure, as a multitude of other possibilities. Note that in both the above, the resulting records still contain all the original newlines. | [reply] [d/l] [select] |