Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Questions and Answers >> Counting unique members of a set
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1473245483

Message started by Gordon Ferrier on Sep 7th, 2016, 6:51am

Title: Counting unique members of a set
Post by Gordon Ferrier on Sep 7th, 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

Title: Re: Counting unique members of a set
Post by Mark Anderson on 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!

Title: Re: Counting unique members of a set
Post by Mark Bernstein on 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.

Title: Re: Counting unique members of a set
Post by Mark Anderson on 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.

Title: Re: Counting unique members of a set
Post by Mark Anderson on 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!).

Title: Re: Counting unique members of a set
Post by Gordon Ferrier on 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

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.