Tinderbox v9 Icon

List/Set.count()


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Last Altered: 

 Property  [other Property type actions]

 List  [operators of similar scope]

 Data manipulation  [other Data manipulation operators]

 Baseline

 As at baseline


Syntax note: Operators without any defined mandatory arguments may omit their empty closing parentheses


List/Set.count()

List/set.count

The property .count counts the Number of discrete items in the specified List or Set data type attribute.

This is better used instead of count(list) or List/Set.size.

The subject list is evaluated so can use a literal list or $attribute(note). It can also use more complex expressions to get data as long as the result is an attribute of the List or Set data type.

For example if $DisplayedAttributes for the current note is "Color;AccentColor;NameFont" then the code

$MyNumber = $DisplayedAttributes.count; 

is effectively

$MyNumber = ("Color;AccentColor;NameFont").count; 

and not surprisingly returns 3. Note that the count is not all unique values for the attribute across the whole TBX; scope is restricted to 'this' note or another nominated note. Specimen usage:

$MyNumber = $DisplayedAttributes.count; 

$MyNumber = $DisplayedAttributes("some other note").count; 

To use .count with a list of items that are attributes or expressions, use list() to pre-create a list:

Works: $MyNumber = list(4+2,9+6).count; (output: 2)

For more complex examples, where list items are action code expressions, it may be necessary to use eval() to wrap each list item expression e.g. list(eval(expressionA),eval(expressionB)).

Examples

The following is a trivial example (given you could use $ChildCount instead) but shows how count can be used in a more subtle way:

$MyNumber = collect(children,$Name).count; 

The result of collect() is a List, in this case a number of note titles. List.count will return the number of values in the list (including duplicates). To get a de-duped count, chain the .unique function before the .count:

$MyNumber = collect(children,$Name).unique.count; 

Similarly, find() returns a List but note that find() does not de-dupe for aliases. Thus test $IsAlias in the query to weed alias results from the returned list:

$MyNumber = find($Prototype=="pProject"&$IsAlias==false&$ChildCount>1).count;