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

  • 本文关键字:字符 数字 计算 intersystems-cache mumps
  • 更新时间 :
  • 英文 :


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

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

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

希望这有帮助!

相关内容

  • 没有找到相关文章

最新更新