in reply to print line

If I've understood what you want to achieve (showing the output you expect would help), the following may do the trick:

use strict; use warnings; my %tags; my $printing; while (defined (my $line = <DATA>) || defined $tags{IT}) { if (defined $line) { my ($tag) = $line =~ /^{(\w+)}/; if (! $tag) { print $line if $printing; next; } $printing = $tag =~ /^(TAG|ID)$/; if ($tag ne 'IT') { ++$tags{$tag}; next; } } print $tags{IT} if (! $tags{LINE} || ! $tags{ID}) && defined $tags{IT}; %tags = (IT => $line); } __DATA__ {IT} 343 1 {DATE} 090104 {LINE} LEGISLATORS VISIT SENIOR APARTMENT COMPLEX {TAG} 1lutherridge0104.ART {ID} 234 {IT} 434 2 {DATE} 090104 {LINE} LEGISLATORS VISIT SENIOR APARTMENT COMPLEX

Prints:

1lutherridge0104.ART 234 {IT} 434

The key is to 'remember' what has been seen and perform the extra processing when the next section is detected. Note the tricky stuff that gets the last section handled correctly when the end of file is reached.


True laziness is hard work