|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > String commands > !STR.- String Command > Instring - Find Commands > String Operations |
MiniRobotLanguage (MRL)
STR.CountAny
Counts Occurrences of Individual Characters in a String
Intention
This command is used to count the total occurrences of individual characters specified in a match string (P2) within a source string (P1). Each unique character in P2 is counted separately across the entire source, with the total sum returned.
The search is case-sensitive, so 'A' and 'a' are treated as distinct. Duplicates in P2 are ignored (e.g., "BDB" counts 'B' and 'D' only once each). The count is the sum of occurrences for each unique char in P2.
The result is a numeric count pushed to the Top of Stack (TOS) if no result variable is specified, or stored in an optional variable (P3). If no matches or P2 is empty, returns 0.
This is useful for character frequency analysis, validating input (e.g., count vowels), or text processing tasks in scripts.
Schematic (Individual Char Count)
Source: "ABCDABCD"
Match: "BDA"
Unique Chars: 'B' (occurs 2x), 'D' (2x), 'A' (2x)
Total: 2 + 2 + 2 = 6 (ignores duplicates in match)
For "BDB": 'B' (2x), 'D' (2x) --> Total: 4
Syntax
STR.CountAny|P1|P2[|P3]
Parameter Explanation
•P1 - (Source String) The main string in which to count occurrences. Can be a variable (e.g., $$SRC) or literal.
•P2 - (Match String) String of characters to count individually (case-sensitive, uniques only).
•P3 - (Optional Result Variable) Variable to store the total count. If omitted, result is pushed to TOS.
Examples
' Basic char count (from original sample)
$$SRC=ABCDABCD
$$MAT=BDA
STR.CountAny|$$SRC|$$MAT|$$CNT
MBX.$$CNT = 6 ('B':2, 'D':2, 'A':2)
' Duplicates in match ignored
$$SRC=ABCDABCD
$$MAT=BDB
STR.CountAny|$$SRC|$$MAT|$$CNT
MBX.$$CNT = 4 ('B':2, 'D':2; duplicate 'B' ignored)
' Case sensitivity: Mixed case
$$SRC=AaBbCc
$$MAT=abc
STR.CountAny|$$SRC|$$MAT|$$CNT
MBX.$$CNT = 3 ('a':1, 'b':1, 'c':1; lowercase only)
' Count vowels in sentence
$$SRC=The quick brown fox jumps over the lazy dog
$$MAT=aeiou
STR.CountAny|$$SRC|$$MAT|$$CNT
MBX.$$CNT = 11 (lowercase vowels only)
'----------------------------------------------------
' =================================================================
' SELF-VALIDATING TEST SCRIPT for STR.CountAny (Case-Sensitive Individual Char Count)
' Purpose: Verify all features using IVV. for automated checks
' and a final summary report. Tests basic counts, case-sens, duplicates in P2,
' no match, edge cases (empty, result to TOS/var).
' =================================================================
' --- Initialize Pass/Fail counters ---
$$PAS=0
$$FAI=0
STS.CLEAR
PRT. ===================================================
PRT. 1. BASIC CHAR COUNT TEST
PRT. ===================================================
$$SRC=ABCDABCD
$$MAT=BDA
PRT. Test 1.1: Basic count (to var)...
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=6
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
PRT. Test 1.2: Basic count (to TOS)...
STR.CountAny|$$SRC|$$MAT
$$TOS=#tos#
IVV.$$TOS=1
POV.$$CNT
$$EXP=6
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
ELS.
$$MSG= -> FAIL - Expected stack size: 1, Actual: $$TOS
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. ===================================================
PRT. 2. DUPLICATES IN MATCH TEST
PRT. ===================================================
$$SRC=ABCDABCD
$$MAT=BDB
PRT. Test 2.1: Duplicates ignored...
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=4
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. ===================================================
PRT. 3. CASE SENSITIVITY TEST
PRT. ===================================================
$$SRC=AaBbCc
$$MAT=abc
PRT. Test 3.1: Lowercase only...
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=3
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
$$MAT=ABC
PRT. Test 3.2: Uppercase only...
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=3
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. ===================================================
PRT. 4. VOWEL COUNT TEST
PRT. ===================================================
$$SRC=The quick brown fox jumps over the lazy dog
$$MAT=aeiou
PRT. Test 4.1: Count lowercase vowels...
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=11
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. ===================================================
PRT. 5. NO MATCH TEST
PRT. ===================================================
$$SRC=Hello World
$$MAT=xyz
PRT. Test 5.1: No match...
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=0
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. ===================================================
PRT. 6. EDGE CASE TESTS
PRT. ===================================================
PRT. Test 6.1: Empty source string...
$$SRC=
$$MAT=hello
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=0
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. Test 6.2: Empty match string...
$$SRC=Hello World
$$MAT=
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=0
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. Test 6.3: Special characters...
$$SRC=Hello, World!
$$MAT=,!
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=3
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. Test 6.4: All unique in match...
$$SRC=abcdefghijklmnopqrstuvwxyz
$$MAT=abc
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=3
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. Test 6.5: Match with duplicates in source...
$$SRC=aaabbbccc
$$MAT=abc
STR.CountAny|$$SRC|$$MAT|$$CNT
$$EXP=9
IVV.$$CNT=$$EXP
PRT. -> PASS
VIC.$$PAS
ELS.
$$MSG= -> FAIL - Expected: $$EXP, Actual: $$CNT
PRT.$$MSG
VIC.$$FAI
EIF.
STS.CLEAR
PRT. ===================================================
PRT. TEST SUMMARY
PRT. ===================================================
CAL.$$TOT=$$PAS+$$FAI
$$MSG=Total Tests: $$TOT
PRT.$$MSG
$$MSG=Passed: $$PAS
PRT.$$MSG
$$MSG=Failed: $$FAI
PRT.$$MSG
' --- Display final result message box ---
IVV.$$FAI=0
MBX.SUCCESS: All tests passed!|Test Result|64
ELS.
$$MSG=FAILURE: $$FAI of $$TOT tests failed.
MBX.$$MSG|Test Result|16
EIF.
ENR.

Remarks
-The count is case-sensitive and treats each unique char in P2 separately.
-Duplicates in P2 are ignored; count is sum of unique chars' occurrences.
-If P2 is empty, returns 0.
-Result is numeric (long integer type); use GVT. to check type if needed.
-For case-insensitive, use VTU. or VTL. on strings before calling, or a custom macro.
-For substring counts (not chars), use STR.Count.
Limitations
-Case-sensitive only; no built-in insensitive flag.
-Counts individual unique chars, not substrings; for substrings, use STR.Count.
-No support for multi-char patterns or regex; for advanced, use loops with STR.Find.
See also: