Skip to main content
Previous sectionNext section

COUNTS

Returns the number of instances of a substring in each element of a dynamic array.

Synopsis

COUNTS(dynarray,substring)

Arguments

dynarray The array of elements that are to be searched for instances of substring. An expression that resolves to a dynamic array.
substring A substring to match against each element in dynarray. An expression that resolves to a string.

Description

The COUNTS function returns the number of times a specified substring appears in each element of dynarray. These values are returned as a dynamic array of integer counts. A missing dynarray element or an element containing the empty string ("") always returns a count of 0.

String matching is case-sensitive. Numbers are converted to canonical form, with leading and trailing zeroes and plus signs removed. Numeric strings are not converted to canonical form.

If a dynamic array element is an empty string ("") or a missing element, COUNTS returns a count of 0 for that element. If substring is an empty string, COUNTS returns a count for each element equal to the number of characters in that element.

Examples

The following example uses the COUNTS function to return the number of appearance of a substring in each element of a dynamic array:

citystate="Springfield IL":@VM:"Springfield MA":@VM:
"Somerville MA":@VM:"Somerville NJ":@VM:"Somerville ME"
PRINT COUNTS(citystate,"Somerville")
PRINT COUNTS(citystate,"Springfield")
PRINT COUNTS(citystate,"MA")
PRINT COUNTS(citystate,"VA")
Copy code to clipboard

The following example returns the count of the zeros in each element. Conversion of numbers to canonical form eliminates leading and trailing zeros. Numeric strings are not converted to canonical form. The missing element and the null string element return 0 regardless of the substring value:

nums=000.1:@VM:0:@VM:@VM:"":@VM:0123.00:@VM:1230:@VM:"007.00"
PRINT COUNTS(nums,0); ! Returns 0ý1ý0ý0ý0ý1ý4 
Copy code to clipboard

The following example specifies the null string as the substring value. It returns the count of characters in each element. Conversion of numbers to canonical form eliminates leading and trailing zeros. Numeric strings are not converted to canonical form. The missing element and the null string element return 0:

nums=000.1:@VM:0:@VM:@VM:"":@VM:0123.00:@VM:1230:@VM:"007.00"
PRINT COUNTS(nums,""); ! Returns 2ý1ý0ý0ý3ý4ý6 
Copy code to clipboard

See Also