计算每个字符出现的次数,即每个字母、数字和标点字符使用了多少次

Count the number of occurrences of each character, i.e., how many times each letter, number, and punctuation character is used

我正在尝试编写一个计算全局字符数的例程。

这些是我设置的全局变量和我想要计算的字符。

 s ^XA(1)="SYLVESTER STALLONE, BRUCE WILLIS, AND ARNOLD SCHWARZENEGGER WERE DISCUSSING THEIR "
 s ^XA(2)="NEXT PROJECT, A BUDDY FILM IN WHICH BAROQUE COMPOSERS TEAM UP TO BATTLE BOX-OFFICE IRRELEVANCE "
 s ^XA(3)="EVERY HAD BEEN SETTLED EXCEPT THE CASTING. "
 s ^XA(4)="""ARNOLD CAN BE PACHELBEL,"" STALLONE. ""AND I WANT TO PLAY MOZART. """
 s ^XA(5)="""NO WAY!"" SAID WILLIS. ""YOU'RE NOT REMOTELY MOZARTISH. """ 
 s ^XA(6)="""I'LL PLAY MOZART. YOU CAN BE HANDEL. """
 s ^XA(7)="""YOU BE HANDEL!"" YELLED STALONE. ""I'M PLAYING MOZART! """
 s ^XA(8)="FINALLY, ARNOLD SPOKE ""YOU WILL PLAY HANDEL,"" HE SAID TO WILLIS. "
 s ^XA(9)="""AND YOU,"" HE SAID TO STALLONE, ""THEN WHO ARE YOU GONNA PLAY? """
 s ^XA(10)="""OH YEAH?"" SAID STALLONE, ""THEN WHO ARE YOU GONNA PLAY? """
 s ^XA(11)="ARNOLD ROSE FROM THE TABLE AND DONNED A PAIR OF SUNGLASSES. "
 s ^XA(12)="I'LL BE MOZART."

如果我没有正确理解你的问题,而你只需要全局中所有字符的总数,那么你可以:

    set key = ""
    for {
        set key = $Order(^XA(key))
        quit:key=""
        for i=1:1:$Length(^XA(key)) {
            set char = $Extract(^XA(key), i)
            set count(char) = $get(count(char)) + 1
        }
    }
    zwrite count // or just return count

对于您的示例,这将产生以下输出:

count(" ")=112
count("!")=3
count("""")=24
count("'")=4
count(",")=9
count("-")=1
count(".")=11
count("?")=3
count("A")=54
count("B")=12
count("C")=13
count("D")=23
count("E")=60
count("F")=6
count("G")=8
count("H")=20
count("I")=28
count("J")=1
count("K")=1
count("L")=48
count("M")=11
count("N")=39
count("O")=44
count("P")=13
count("Q")=1
count("R")=28
count("S")=29
count("T")=33
count("U")=13
count("V")=3
count("W")=11
count("X")=3
count("Y")=21
count("Z")=6

希望对您有所帮助!