User Tools

Site Tools


Sidebar

Passage markup

basics

coding

extra

list

section

whitespace

Macros

basics

colour

data structure

date and time

game state

live

maths

number

revision

saving

showing and hiding

string

styling

url

deprecated

Types of data

Special keywords

Special passage tags

harlowe:count

(count: array or String, …Any) → Number

Accepts a string or array, followed by a value, and produces the number of times any of the values are inside the string or array.

Example usage:

(count: (a:1,2,3,2,1), 1, 2) produces 4. (count: "Though", "ugh","u","h") produces 4.

Rationale:

You can think of this macro as being like the contains operator, but more powerful. While contains produces true or false if occurrences of the right side appear in the left side, (count:) produces the actual number of occurrences.

Note that if you only want to check if an array or string contains any or all of the values, it's easier to use contains with the all property like so: $arr contains all of (a:1,2) and $arr contains any of (a:1,2). But, if you need an exact figure for the number of occurrences, this macro will be of use.

Details:

If you use this with a number, boolean, datamap, dataset (which can't have duplicates), or anything else which can't have a value, then an error will result.

If you use this with a string, and the values aren't also strings, then an error will result.

Substrings are counted separately from each other - that is, the string “Though” contains “ugh” once and “h” once, and (count: "Though","ugh","h") results in 3. To check for “h” occurrences that are not contained in “ugh”, you can try subtracting two (count:)s - (count: "Though","ugh") - (count: "Though","h") produces 1.

See also:

harlowe/count.txt · Last modified: 2017/06/18 22:14 by l