New27
New27

Reputation: 39

Count the number of occurrences of each character in a string

Tried the solution that ZitRo posted, but i have the error - Inconsitent use of QUITs within the tag. Any ideas how i can fix this?

n key,char,i
s ary(1)="abcabc aba"
s key =""
f{
  s key=$Order(ary(key))
  i key="" q
  f i=1:1:$L(ary(key)){
    set char=$E(ary(key),i)
    set countByChar(char)=$get(countByChar(char))+1}}   
q countByChar

Upvotes: -1

Views: 100

Answers (1)

New27
New27

Reputation: 39

I managed to figure it out. The error message was caused by two quits in the code; one quit with a value and one quite without a value.

Final code:

n key,char,i,total
s ary(1)="abcabc aba"
s ary(2)=123    
s key=""
s total=0
f  s key=$Order(ary(key)) q:key=""  d
.  f i=1:1:$L(ary(key)) d
.  .  s char=$E(ary(key),i)
.  .  s countByChar(char)=$get(countByChar(char))+1
.  .  s total=total+1
zwrite countByChar 
w total

Upvotes: 1

Related Questions