Updated 2018-01-08 10:58:58 by CecilWesterhof

Created by CecilWesterhof.

At a certain point I needed the maximum, minimum and mean of a list. Instead of calculating them one after the other I created a proc that calculates everything in one go. And also length and sum. (But KPV pointed out that there is ::math::statistics::basic-stats. So I do not need this any-more.

This is the proc:
proc listNumericStats {thisList} {
    set length [llength ${thisList}]
    if {${length} == 0} {
        error "List cannot be empty"
    set max [lindex ${thisList} 0]
    set min ${max}
    set sum 0.
    foreach element ${thisList} {
        set sum [expr {${sum} + ${element}}]
        if {${element} < ${min}} {
            set min ${element}
        } elseif {${element} > ${max}} {
            set max ${element}
    dict set stats length ${length}
    dict set stats max    ${max}
    dict set stats mean   [expr {${sum} / ${length}}]
    dict set stats min    ${min}
    dict set stats sum    ${sum}
    return ${stats}

DEC:That sounds over kill to me
set aList [ lsort -real $anotherList]
set max [ lindex $aList end]
set min [ lindex $aList 0]
set sum [expr {[join $aList "+"]}]
set len [llength $aList ] 
set mean [ expr {$sum/$len}]

DEC:I would avoid looping over a list at the script level if you can

CecilWesterhof: With your code you loop two times over the list instead of once. And you need a sort also, which is also not cheap for big lists. So I would think that my implementation is a lot more efficient.

But it is mute, because I do not need it any-more thanks to KPV.

KPV: Also checkout ::math::statistics::basic-stats

[Category Numerics]