my $last; my @history; my $expect = 1; while (<>) { chomp; my ($num) = /EVENT(\d+)/ or next; # Ignore bad input. next if defined($last) && $last eq $_; $last = $_; if ($num == 1 || $num != $expect) { @history = (); $expect = 1; } if ($num == $expect) { push @history, "$_\n"; if ($expect++ == 4) { print(@history); @history = (); $expect = 1; } } }