这个函数的要求如下:
In this exercise, you’ll create a function that counts the number of occurrences of each character in a paragraph.
Create a function named countCharsInAry(ary,countByChar):
ary is the array of strings to count. Assume it only has one subscript level, and the value at each subscript contains one line from the paragraph.
countByChar is an output array where the subscript is a character from the paragraph, and the value is the number of times that character was found in the paragraph.
Quit from countCharsInAry with the total number of characters from ary.
测试需要使用的用例。
1 - One line that has a numeric subscript at 1
*Data: ary(1)="abcabc aba"
*Test: s total=$$countCharsInAry^XCHR310EX3YCH(.ary,.countByChar)
*total has been incorrectly set to 1 but expected 10
*countByChar value has been incorrectly set to 0 but expected ""
*countByChar at subscripts (" ") has no data in this node, but expected data
with value 1
*countByChar at subscripts ("a") has no data in this node, but expected data
with value 4
*countByChar at subscripts ("b") has no data in this node, but expected data
with value 3
*countByChar at subscripts ("c") has no data in this node, but expected data
with value 2
2 - One line with a numeric subscript > 1
*Data: ary(10)="abcabc aba"
*Test: s total=$$countCharsInAry^XCHR310EX3YCH(.ary,.countByChar)
*total has been incorrectly set to 1 but expected 10
*countByChar value has been incorrectly set to 0 but expected ""
*countByChar at subscripts (" ") has no data in this node, but expected data
with value 1
*countByChar at subscripts ("a") has no data in this node, but expected data
with value 4
*countByChar at subscripts ("b") has no data in this node, but expected data
with value 3
*countByChar at subscripts ("c") has no data in this node, but expected data
with value 2
3 - Two lines with numeric subscripts
*Data: ary(1)="abcabc aba",ary(2)=123
*Test: s total=$$countCharsInAry^XCHR310EX3YCH(.ary,.countByChar)
*total has been incorrectly set to 1 but expected 13
*countByChar value has been incorrectly set to 0 but expected ""
*countByChar at subscripts (1) has no data in this node, but expected data
with value 1
*countByChar at subscripts (2) has no data in this node, but expected data
with value 1
*countByChar at subscripts (3) has no data in this node, but expected data
with value 1
*countByChar at subscripts (" ") has no data in this node, but expected data
with value 1
*countByChar at subscripts ("a") has no data in this node, but expected data
with value 4
*countByChar at subscripts ("b") has no data in this node, but expected data
with value 3
*countByChar at subscripts ("c") has no data in this node, but expected data
with value 2
4 - One line with an alphabetic subscript
*Data: ary("A")="abcabc aba"
*Test: s total=$$countCharsInAry^XCHR310EX3YCH(.ary,.countByChar)
*total has been incorrectly set to 1 but expected 10
*countByChar value has been incorrectly set to 0 but expected ""
*countByChar at subscripts (" ") has no data in this node, but expected data
with value 1
*countByChar at subscripts ("a") has no data in this node, but expected data
with value 4
*countByChar at subscripts ("b") has no data in this node, but expected data
with value 3
*countByChar at subscripts ("c") has no data in this node, but expected data
with value 2
5 - Two lines with alphabetic subscripts
*Data: ary("A")="abcabc aba",ary("B")=123
*Test: s total=$$countCharsInAry^XCHR310EX3YCH(.ary,.countByChar)
*total has been incorrectly set to 1 but expected 13
*countByChar value has been incorrectly set to 0 but expected ""
*countByChar at subscripts (1) has no data in this node, but expected data
with value 1
*countByChar at subscripts (2) has no data in this node, but expected data
with value 1
*countByChar at subscripts (3) has no data in this node, but expected data
with value 1
*countByChar at subscripts (" ") has no data in this node, but expected data
with value 1
*countByChar at subscripts ("a") has no data in this node, but expected data
with value 4
*countByChar at subscripts ("b") has no data in this node, but expected data
with value 3
*countByChar at subscripts ("c") has no data in this node, but expected data
with value 2
源代码
代码的内容不是很长,主要是需要使用了嵌套循环。
; https://www.isharkfly.com/t/mumps-character-counting/15224
SET ary("1")="abcabc aba"
SET ary("2")="123"
;SET ary("11")="abcabc aba"
DO countCharsInAry(.ary, .countByChar)
set name=""
f s name=$O(countByChar(name)) q:name="" w !,name,">",countByChar(name)
QUIT
countCharsInAry(ary,countByChar)
n charIndex
n currentChar
n name
n str
n totalChars
S name=""
S totalChars=0
FOR SET name=$O(ary(name)) QUIT:name="" D
. S str=ary(name)
. S countByChar=""
. FOR charIndex=1:1:$LENGTH(str) DO
. . S currentChar=""
. . S currentChar=$EXTRACT(str,charIndex)
. . IF $DATA(countByChar(currentChar))'=1 DO
. . . SET countByChar(currentChar)=1
. . ELSE DO
. . . SET countByChar(currentChar)=countByChar(currentChar)+1
. . SET totalChars=totalChars+1
w !,totalChars
Q totalChars
程序调试的输出: