#!/usr/bin/perl use strict; use warnings; use Data::Dumper; $|=1; # Don't read in another line if we are still working # on a START line. This is caused by the # X START Y syntax in conjunction with the idea # of END absent a START in this example file format. # As a thought redefining the input separtator to # be 'START' could possibly be productive if the format # is not exactly like this?, # This format has some of the nastiest things to deal # with. They normally do not occur all at once! my @record=(); my $line_in =''; while ( $line_in =~ /START/ or $line_in =) { $line_in = construct_record($line_in) if $line_in =~ /START/; } sub construct_record { my $line = shift; if ( (my $x) = $line =~ /START\s+(\w+)\s*$/) { push @record, $x; } while (defined ($line = ) and ($line !~ /(START|END)/) ) { $line =~ s/^\s*|\s*$//g; push @record, $line; } $line //= ''; #could be an EOF if (my ($b4end) = $line =~ /^ (?: (.+) \s+ )? END $/x) { push @record, $b4end if $b4end; output_record(); return ''; # no continuation of this record } if ( my ($x,$y) = $line =~ /^ (?: (.+) \s+ )? START (?: \s+ (.+) )? $/x ) { if ($x) { push @record, $x; output_record(); } if ($y) { output_record(); # might be: "^START 77"? return "START $y"; } } return ''; } sub output_record # or process it somehow... { print "Record: @record\n" if (@record >1); @record=(); } =Prints Record: a b Record: c d e f g Record: h i Record: j k =cut __DATA__ START a b START c d e f g END ignoreme START h i START j k END