The general problem is a file where each line is comprised of identifiable fields but a "record" spans multiple lines. There is some key field that has the same value repeated on each line of the record. The process typically starts with externally sorting the file so that all lines of the record are adjacent.
Then it is simply a matter of pushing all the matching into an array and then processing the array as soon as all the lines for that record have been read.
Of course, parse_line() is usually overkill because the line is delimited in such a way that split is sufficient. Here are some things that may not be so obvious:#!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0] or die "Usage: $0 <input_file>"; open(my $fh, '<', $file) or die "Unable to open '$file' for reading: $ +!"; my ($curr_key, @rec) = ('', ()); while (<$fh>) { chomp; my $entry = parse_line($_); if ($entry->{key} ne $curr_key) { process_rec($curr_key, \@rec); ($curr_key, @rec) = ($entry->{key}, $entry); } else { push @rec, $entry; } } process_rec($curr_key, \@rec); sub parse_line { my ($line) = @_; my %entry; # ... return \%entry; } sub process_rec { my ($key, $rec) = @_; return if ! @$rec; # ... }
Cheers - L~R
In reply to Process Records Spread Across Multiple Lines by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |