Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Counting unique members of a set (Read 1107 times)
Gordon Ferrier
Full Member
*
Offline



Posts: 6

Counting unique members of a set
Sep 07th, 2016, 6:51am
 
I have a group of notes in a container that have a set-type attribute "Codes". I'd like to count the number of distinct occurrences of each code throughout that group. My sense from other environments is that I want to iterate through the notes group but don't know how to do that in TBX. And there may be a better way to do it in any event.

TIA

Gordon
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Counting unique members of a set
Reply #1 - Sep 7th, 2016, 7:56am
 
$MyNumber = $MySet.count

See also List.set.count, List/Set.size and count(list).

The .count for Lists and sets was added in v6.4.0, essentially as a more intuitive version of .size, which seems to do the same thing.  Both dot operators also do the same ass the older count() method.  So, you've a choice of three!
Back to top
 
« Last Edit: Sep 7th, 2016, 7:57am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Counting unique members of a set
Reply #2 - Sep 7th, 2016, 11:34am
 
Attribute Browser is handy for this.  For each distinct Code, you can see which notes use that code and the number of notes using the code.
Back to top
 
 
WWW   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Counting unique members of a set
Reply #3 - Sep 7th, 2016, 12:51pm
 
I perhaps misconstrued the question. MB's spot on - as I write, I'm using Attribute Browser to visually check per-value counts.

However, AB view will show you those counts but can't export them (it's not intended for that). So if you need to somehow get a list of values en route to saving per-value counts, you may also want to look at values(). This will give you the unique values for the attribute across the whole document. If you need a list with tighter scope use collect() with .unique (as collect() returns a List). You can then use .each() to iterate over the list of values, doing a collect(value).unique.count for each.

But if eyeballing the values is enough, Attribute Browser is the way to go.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Counting unique members of a set
Reply #4 - Sep 7th, 2016, 1:01pm
 
Ooh - it works. I just tried the above. I wanted a to get a list of counts of unvalued used in user string attribute $FlagType:

values("FlagType").each(X){
  $MyNumber=;
  $MyNumber=sum_if(find($FlagType),X==$FlagType,1);
  $MySet=$MySet+(X+":"+$MyNumber);
  $MyNumber=;
};
$Text=$MySet.format("\n");
$Text=$Text.replace(":",": ");


I get text like:

abuse filter: 5
blacklist: 2
books: 2
bots: 2
creation: 8
edits: 6
...


Actually, if I'd used +": "+ in the $MySet= line of the loop I could have avoided the extra step at the end with .replace() to prettify the text. But, I was interested to also try making a look-up table list.

I'd be cautious doing this with big documents and complex queries. My doc has 2.6k notes, 1 agent (turned off), and 1 rule (the one above!).
Back to top
 
« Last Edit: Sep 7th, 2016, 1:02pm by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Gordon Ferrier
Full Member
*
Offline



Posts: 6

Re: Counting unique members of a set
Reply #5 - Sep 8th, 2016, 5:50am
 
Many thanks both Marks for these very helpful and comprehensive suggestions. Lots of food for thought here for my current and potential future needs.
Gordon
Back to top
 
 
  IP Logged
Pages: 1
Send Topic Print