in reply to summary statistics using Data::Table

I've never used Data::Table. But I skimmed its documentation, installed it and was able to implement a solution using the colsMap method:
#! /usr/bin/perl use warnings; use strict; use List::Util qw{ sum }; use Data::Table; my $t = new Data::Table( [ ['a', 1000, 2000, 3000, 200,500], ['b', 2000, 1000, 1000, 700,800], ['c', 3000, 3000, 3000, 5,7], ], ['Name', 'value1', 'value2', 'value3', 'value4', 'value5'], 0); $t->addCol(undef, 'v1-3'); $t->colsMap(sub { $_->[-1] = sum(@$_[1 .. 3]) / 3 }); $t->addCol(undef, 'v4,5'); $t->colsMap(sub { $_->[-1] = sum(@$_[4, 5]) / 2 }); print $t->csv;

Or, more DRY:

sub set_avg { my @cols = @_; sub { $_->[-1] = sum(@$_[@cols]) / @cols } } $t->addCol(undef, 'v1-3'); $t->colsMap(set_avg(1 .. 3)); $t->addCol(undef, 'v4,5'); $t->colsMap(set_avg(4, 5));
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: summary statistics using Data::Table
by sisterdot (Initiate) on Dec 14, 2015 at 07:56 UTC

    Thanks a lot charob! i like DRY :-)

    here are suggestion i got which use the colnames:

    Version1:

    my @mean123=map( ($t->elm($_,'value1')+$t->elm($_,'value2')+$t->elm($_ +,'value3'))/3.0, 0..$t->nofRow()-1); $t->addCol(\@mean123, 'Mean123'); print $t->csv;

    Version2:

    $t->addCol(undef, 'Mean45'); # add a new column for (my $i=0; $i< $t->nofRow; $i++) { $r=$t->rowHashRef($i); $t->setElm($i, 'Mean45', ($r->{'value4'}+$r->{'value5'})/2.0); } print $t->csv;
    all works super fine- thanks a lot for your help!