intersystems-cachemumps

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


I am trying to write a routine that counts the characters in a global.

These are the globals I set and the characters I would like counted.

 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."

Solution

  • If I understood your question correctly, and you just need the total count of all characters in a global, here you go:

        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
    

    As for your example, this will produce the following output:

    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
    

    Hope this helps!