Re: Looking for elegant summary function
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Looking for elegant summary function

From: Aleks <vuje@ifg.uni-hannover.de>
Date: Thu Dec 08 2005 - 15:30:06 CET

Christopher Nelson schrieb:
> I'm looking for an elegant and efficient way to determine how many
> distinct elements are in a list. For example [x [list 1]] and [x
> [list 1 1]] and [x [list 1 1 1 1]] should all return 1, [x [list 1 2]]
> and [x [list 1 2 1]] should return 2. One brute force approach that
> suggests itself (untested) is:
>
> proc x { l } {
> foreach e $l {
> set a($e) 1
> }
> return [llength [array names a]]
> }
>
> Not too bad, I guess. Any other thoughts?
>
> Chris
>

Probably not very helpful but tclx has a function (lrmdups) wich removes
duplicate values from lists so you could say:

llenght [lrmdups "1 2 2 3 3"]
=> 3

Aleks
Received on Sun Dec 11 13:54:19 2005