use List::Util 'sum'; while () { my @fields = split /\t/; my $identifier = shift @fields; my ($category, $group) = ($identifier =~ /^TS(\d+)(\w+)/); warn "Parse error", return unless $category && $group; # This may not be what you're doing, but change # this to do the calculations you need to do $sums{$group}->{$category} += sum(@fields); $totals{$group}->{$category} += @fields; } # Another way of cleaning up bad data foreach (keys %sums) { $sums{$_}->{CCFN30NT} += delete $sums{$_}->{CCCFN30NT}; $totals{$_}->{CCFN30NT} += delete $totals{$_}->{CCCFN30NT}; ... }