Tinderbox v9 Icon

count(scope)


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Last Altered: 

Operator Has Newer Dot-Operator Variant: 

 Function  [other Function type actions]

 List  [operators of similar scope]

 Data manipulation  [other Data manipulation operators]

 Baseline

 As at baseline

 [More on scoped arguments in Action Code]

Yes


count(scope)

The function count() counts the Number of discrete items in the specified ist scope which defined as a group of items—i.e. one or more items.

Most often, list may be a reference to a List or Set typed data. The list argument is evaluated so can use more than literal lists, including offset references like $Attribute(note) or more complex expressions to get data as long as the result is a list-based attribute (List or Set data types).

NOTE: where scope is a known List or Set type attribute reference, it is recommended and generally easier, and recommended, to use List/Set.count (or alternatively the older less intuitive List/Set.size); both the latter give the same outcome.

count(scope)

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

$MyNumber = count($DisplayAttributes); 

is effectively

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

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 = count($DisplayAttributes); 

$MyNumber = count($DisplayAttributes("some other note")); 

To use count() with a list of items that are attributes or expressions, use list():

Works: $MyNumber = count(list(4+2,9+6)); (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 we could use $ChildCount instead) but shows how count can be used in a more subtle way:

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

The result of collect is List-type data, in this case a number of note titles. count(list) will return the number of values in the list (including duplicates). To get a de-duped count, chain the .unique operator to the list reference, inside the count() operator so the .unique filter is applied before the count is taken:

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