This shows you the differences between two versions of the page.
— |
harlowe:count [2017/10/09 20:39] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | **(count: // [[harlowe:Array|array]] or [[harlowe:String|String]], ...Any//) -> //[[harlowe:Number|Number]]//** | ||
+ | Accepts a [[harlowe:string|string]] or [[harlowe:array|array]], followed by a value, and produces the [[harlowe:number|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, [[harlowe:boolean|boolean]], [[harlowe:datamap|datamap]], [[harlowe:dataset|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:datanames|(datanames:)]], [[harlowe:datavalues|(datavalues:)]] |