while (<$FILE>){ if (/^\d/){ # new entry, sum values with those of $hash{$id}}and increment $hash{$id}->count } elsif(/^\s+ID/){ @fields=split /\s+/; $id=$fields[3]; } ...