#! perl -sw use strict; my @data = ; my ($type3total, $type4total) = (0,0); my $state = ''; for (@data) { chomp; my ($part1, $part2) = split; if ($state eq "type4" && $part1 eq "3") { process( $type3total, $type4total ); ($type3total, $type4total) = (0,0); } $state = "type$part1"; $type3total += $part2 if ($part1 == 3); $type4total += $part2 if ($part1 == 4); die "array contains a type 7 record\n" if $part1 == 7; } process( $type3total, $type4total ); sub process { my ($type3, $type4) = (shift, shift); print "Processing type 3 total of $type3 and a type 4 total of $type4\n"; return; } __DATA__ 3 200 4 50 3 100 3 100 3 100 4 50 4 25