Chunk Processor 1 1 1 2 1 2 1 3 1 0 1 2 f 1 f 2 f 4 f 4 s 6 s 3 s 2 s 1 f 3 g 2 g 6 #### use strict; use warnings; my $chunk_old; my @processor_nums; while (my $line = ){ my ($chunk, $processor_num) = $line =~ m{(\w*\d*)\s+(\d+)}; if (!defined $chunk_old){$chunk_old = $chunk}; if ($chunk eq $chunk_old){ push @processor_nums, $processor_num; #still on the same chunk, append processor number to list. } else { my $result = do_calculation(\@processor_nums); #we've got all processor numbers for this chunk, now calculate. print "$result\n"; #or do something else with it... @processor_nums = (); #empty the list, as we're starting with the new chunk. push @processor_nums, $processor_num; #save the processor number we've read but not yet used in calculation $chunk_old = $chunk; #update the chunk flag to the new chunk value } } sub do_calculation { my $processor_list_ref = shift; my $total = 0; for my $value(@$processor_list_ref){ #do calculation stuff here.... $total = $total + $value; } return $total; #whatever result(s) you get } __DATA__ 1 1 1 2 1 2 1 3 1 0 1 2 f 1 f 2 f 4 f 4 s 6 s 3 s 2 s 1 f 3 g 2 g 6