Ich wollte mich mal an "count_chars()" von PHP versuchen und war erstaunt, wie einfach man das nachbilden kann.
Code
/* aus PHP:
mixed count_chars ( string $string [, int $mode = 0 ] )
Zählt die Häufigkeit des Vorkommens jedes einzelnen Byte-Wertes (0..255) in string und gibt sie auf verschiedene Arten zurück.
Abhängig von mode gibt count_chars() eine der folgenden Möglichkeiten zurück:
0 - ein Array mit den Byte-Werten als Schlüssel und deren jeweiliger Häufigkeit als Wert.
1 - wie 0, allerdings werden nur Byte-Werte ausgegeben, die mindestens einmal vorkommen.
2 - wie 0, allerdings werden nur Byte-Werte, die nicht vorkommen, aufgelistet.
3 - eine Zeichenkette, die alle vorkommenden Zeichen enthält.
4 - eine Zeichenkette, die alle nicht vorkommenden Zeichen enthält.
*/
Proc php_count_chars
Parameters string S, int mode
Declare int i, a[255], hash H[]
Clear a[]
WhileLoop 1,Len(S) : i = Ord(Mid$(S,&loop,1)) : a[i] = a[i] + 1 : EndWhile
Select mode
CaseOf 0
WhileLoop 1, 255
H[&loop] = Str$( a[&loop] )
EndWhile
Return H[]
CaseOf 1
WhileLoop 1, 255
Case a[&loop] : H[&loop] = Str$( a[&loop] )
EndWhile
Return H[]
CaseOf 2
WhileLoop 1, 255
CaseNot a[&loop] : H[&loop] = Str$( a[&loop] )
EndWhile
Return H[]
CaseOf 3
Clear S
WhileLoop 1, 255
Case a[&loop] : S = S + Chr$(&loop)
EndWhile
Return S
CaseOf 4
Clear S
WhileLoop 1, 255
CaseNot a[&loop] : S = S + Chr$(&loop)
EndWhile
Return S
Otherwise 'CaseOf 0
WhileLoop 1, 255
H[&loop] = Str$( a[&loop] )
EndWhile
Return H[]
EndSelect
EndProc
cls
Declare string a,b, S,S1,S2, hash H[]
S = "Dies ist ein kleiner Teststring."
S1 = "Holla, die Waldfee"
' ---
Print "\n"; S
S2 = php_count_chars(S,3)
Print "alle vorkommenden Zeichen:", S2
H[] = php_count_chars(S,1)
ForEach H[],a,b
Print " ",Chr$(Val(a))," - ",b,"mal"
EndFor
' ---
Print "\n"; S1
S2 = php_count_chars(S1,3)
Print "alle vorkommenden Zeichen:", S2
H[] = php_count_chars(S1,1)
ForEach H[],a,b
Print " ",Chr$(Val(a))," - ",b,"mal"
EndFor
WaitEnd
Alles anzeigen
Ausgabe:
Spoiler anzeigen
-----------------------------------------------------
Dies ist ein kleiner Teststring.
alle vorkommenden Zeichen: .DTegiklnrst
- 4 mal
. - 1 mal
D - 1 mal
T - 1 mal
e - 5 mal
g - 1 mal
i - 5 mal
k - 1 mal
l - 1 mal
n - 3 mal
r - 2 mal
s - 4 mal
t - 3 mal
Holla, die Waldfee
alle vorkommenden Zeichen: ,HWadefilo (first in last out?? in die Wade???)
- 2 mal
, - 1 mal
H - 1 mal
W - 1 mal
a - 2 mal
d - 2 mal
e - 3 mal
f - 1 mal
i - 1 mal
l - 3 mal
o - 1 mal
P.S.: ist'n bisserl geschönt - fällt gar nicht auf