PDA

View Full Version : about TALLY function



primo
14-02-2016, 11:24
i was about to ask a new feature like this
n = Tally(txt, Any "A" | "b" | "z")
does not work
then tried
n = Tally(txt, Any ("A" , "b" , "z"))
does not work
then searching the examples for the word "any", most of the results is "press any key to end"
then fortunately i saw "any $spc & $tab & $nul)" in example ODBC_EX_03, and i said thats it let me try again, and now it works :D
n = Tally(txt, Any "A" & "o" & "B" & "i" &"I" )
n = Tally(txt, Any "A" + "o" + "B" + "i" +"I" )
the two versions works
so my problem solved .
there is a famous basic programming language which have
CountString(txt, "a")
but seems not possible like in thinbasic, it does not accept multichoices inside CountString.

Uses "Console"
String txt
DWord n
txt = "Hello World from ThinBASIC!"
n = Tally(txt, Any "A" & "o" & "B" & "i" &"I" )
'n = Tally(txt, Any "A" + "o" + "B" + "i" +"I" )
'---Print the magic words
PrintL "Hello World from ThinBASIC!"
PrintL "number of characters AoBiI in the above phrase = " + Str$(n)
PrintL "Press a key to end program"
'---Wait for a key press
WaitKey

ReneMiner
14-02-2016, 11:55
as simple as


n = Tally(txt, Any "AoBiI" )

:)

to count single chars or continuing occurences in same order you also could "abuse" ParseCount-function.
subtract 1 since the result usually tells the count of text-parts delimited by the passed chars



Uses "Console"

String txt = "Hello World from ThinBASIC!"
PrintL txt & $CRLF

PrintL "Tally ""ThinBASIC!"" = ", Str$(Tally(txt, "ThinBASIC!"))
PrintL "Tally Any ""ThinBASIC!"" = ", Str$(Tally(txt, Any "ThinBASIC!"))
PrintL "Parsecount ""ThinBASIC!"" - 1 = ", Str$(ParseCount(txt, "ThinBASIC!" )-1)


PrintL $CRLF & "key to end"
WaitKey

primo
14-02-2016, 13:52
you are right ReneMiner , and thanks, just 3 minutes ago i said to myself why this
n = Tally(txt, Any "AoBiI" ) does not come to my mind, i should go to the forum and edit the code .
possibly (and not sure) my hidden idea was a general form which accepts also something like this
n = Tally(txt, Any "He" , "T") ie searching "He" as a whole and "T"
even we could do this using other string functions. the Tally is small and handy to use