Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Min, max and average from a list

935 views
Skip to first unread message

Cecil Westerhof

unread,
Dec 20, 2017, 8:44:07 AM12/20/17
to
When I have a list of floats is there functionality to get the
minimum, maximum and average of the values? Or do I have to write this
myself?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Gerald Lester

unread,
Dec 20, 2017, 9:25:48 AM12/20/17
to
On 12/20/2017 07:41 AM, Cecil Westerhof wrote:
> When I have a list of floats is there functionality to get the
> minimum, maximum and average of the values? Or do I have to write this
> myself?

Read the "mathfunc" man/help page
(http://www.tcl.tk/man/tcl8.6/TclCmd/mathfunc.htm) to answer your question.

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Arjen Markus

unread,
Dec 20, 2017, 9:38:42 AM12/20/17
to
On Wednesday, December 20, 2017 at 3:25:48 PM UTC+1, Gerald Lester wrote:
> On 12/20/2017 07:41 AM, Cecil Westerhof wrote:
> > When I have a list of floats is there functionality to get the
> > minimum, maximum and average of the values? Or do I have to write this
> > myself?
>
> Read the "mathfunc" man/help page
> (http://www.tcl.tk/man/tcl8.6/TclCmd/mathfunc.htm) to answer your question.
>

There is also the math::statistics package in Tcllib.

Regards,

Arjen

Harald Oehlmann

unread,
Dec 20, 2017, 12:14:21 PM12/20/17
to
Here are two ways, not so beginner style, I admitt...

Normally, all tcl operations and functions take only scalar values, no
lists:
% expr min(1,2)
1
So to enter a list, it is a bit difficult and requires text operations.

But all math operations may be accessed as functions without expr and
there, you may use a list as operator, using the delist operator {*}.

So, your operations may be expressed as:

% set l {1 2 3 4 5}
% tcl::mathfunc::min {*}$l
1
% tcl::mathfunc::max {*}$l
5
% expr {double([tcl::mathop::+ {*}$l]) / [llength $l] }
3.0
% expr {[tcl::mathop::+ {*}$l] / [llength $l] }
3

You may consider VecTCL to have a full set of Vector operations.

Cecil Westerhof

unread,
Dec 20, 2017, 1:28:06 PM12/20/17
to
Cecil Westerhof <Ce...@decebal.nl> writes:

> When I have a list of floats is there functionality to get the
> minimum, maximum and average of the values? Or do I have to write this
> myself?

I will look into the suggestions, but I wrote my own function because
I need max, min and mean, so the following is more efficient I think:
http://wiki.tcl.tk/49325

Rolf Ade

unread,
Dec 20, 2017, 4:51:21 PM12/20/17
to

Cecil Westerhof <Ce...@decebal.nl> writes:
>
> I will look into the suggestions, but I wrote my own function because
> I need max, min and mean, so the following is more efficient I think:
> http://wiki.tcl.tk/49325

Reads as:

proc listNumericStats {thisList} {
set length [llength ${thisList}]
if {${length} == 0} {
error "List cannot be empty"
}
set max [lindex ${thisList} 0]
set min ${max}
....

Consider to use the syntax ${varname} only if needed (that is: pretty
seldom). It's good to know this syntax if needed but to use it
everywhere seems like overdueing to me. Why not just:

proc listNumericStats {thisList} {
set length [llength $thisList]
if {$length == 0} {
error "List cannot be empty"
}
set max [lindex $thisList 0]
set min $max
....

Cecil Westerhof

unread,
Dec 20, 2017, 5:28:05 PM12/20/17
to
I prefer to use it always (just like the braces in C++/Java). Reduces
the risks on problems. Did it also always in Bash.

Rolf Ade

unread,
Dec 20, 2017, 6:43:53 PM12/20/17
to

Cecil Westerhof <Ce...@decebal.nl> writes:
> Rolf Ade <ro...@pointsman.de> writes:
>>
>> Consider to use the syntax ${varname} only if needed (that is: pretty
>> seldom). It's good to know this syntax if needed but to use it
>> everywhere seems like overdueing to me. [...]
>
> I prefer to use it always (just like the braces in C++/Java). Reduces
> the risks on problems. Did it also always in Bash.

It's your code and your habit, you're of course free to do what pleases
you.

Though, there are only rare cases (outside of cases like [puts
"foo${bar}grill") where you must use the ${varname} syntax to write a
correct program. It isn't needed at one place in the code we discuss,
for example.

Using this syntax always suggests that this is something needed (which
isn't almost always not the case) or that there are subtil pitfalls to
avoid by carefully always typing ${...} (which isn't true).




0 new messages