#!/usr/bin/perl use strict; use warnings; use List::Util qw/sum/; my $data; sub avg { return sum(@_) / grep { $_ != 0 } @_;}; while () { chomp; push @$data, [@$_, avg( @$_[1..@$_-1])] for ([split(/,/, $_)]); } push @$data, ["TOTAL", map { my $col = $_; avg( map { $_->[$col] } @$data) } 1..11]; print join(",", @$_),"\n" for @$data;