Hello..
I have translated an old Powerbasic example to oxygen.
But where is the Main difference between Tally and Parse?
'
' tally and parse function, oxygen
' hello charles, where is the main different between tally and parse?
'
' translation of a powerbasic example to oxygen
'
FUNCTION Tally(string search, datas ) AS INTEGER
DIM count AS INTEGER = 0 ' Initialize the counter
DIM i AS INTEGER ' Loop variable
' Loop through each occurrence of the delimiter in the data string
FOR i = 1 TO LEN(datas) STEP LEN(search)
' If the substring at this position is equal to the search string, increment the counter
IF MID(datas, i, LEN(search)) = search THEN
count = count + 1
END IF
NEXT i
' Return the count
RETURN count
END FUNCTION
' Example usage
DIM datas AS STRING = "BJF1, BJF2, BJF1, BJF3, BJF1, BJF4"
PRINT "The string 'BJF1' occurs " + STR(Tally("BJF1", datas)) + " times in the data." ' result 3 times
Powerbasic
'
''''''''''''''''''''' powerbasic ''''''''''''''''''''''''''''''''''''''''''
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
DIM sData AS STRING
DIM lNumOccurences AS LONG
DIM lIndex AS LONG
sData = "BJF1, BJF2, BJF1, BJF3, BJF1, BJF4" ' This is your data string
lNumOccurences = 0 ' Initialize the counter
FOR lIndex = 1 TO PARSECOUNT(sData) ' Loop through each parsed element
IF PARSE$(sData, lIndex) = "BJF1" THEN ' If the element is "BJF1"
INCR lNumOccurences ' Increment the counter
END IF
NEXT lIndex
MSGBOX "The string 'BJF1' occurs " + STR$(lNumOccurences) + " times in the data." ' result 3 times
END FUNCTION
''''''''''''''''''''' powerbasic ''''''''''''''''''''''''''''''''''''''''''
The main difference between Tally and ParseCount in PowerBasic is their purpose and how they handle strings.
Tally
The Tally function is used to count the occurrences of a specific substring within a larger string. It simply counts how many times the search substring appears in the main string.
Syntax:
result = TALLY(mainString, searchString)
mainString: The string in which you want to count occurrences.
searchString: The substring you are looking for.
Example:
DIM mainString AS STRING
DIM searchString AS STRING
DIM count AS LONG
mainString = "PowerBasic is powerful. Basic is easy to learn."
searchString = "is"
count = TALLY(mainString, searchString)
PRINT "Occurrences: "; count
ParseCount
The ParseCount function, on the other hand, is used to count the number of tokens (substrings) in a string that are separated by a specified delimiter. It doesn't count occurrences of a specific substring but instead counts how many pieces the string can be divided into based on the delimiter.
Syntax:
result = PARSECOUNT(mainString, delimiter)
mainString: The string you want to split into tokens.
delimiter: The character or string that separates the tokens.
Example:
DIM mainString AS STRING
DIM delimiter AS STRING
DIM tokenCount AS LONG
mainString = "PowerBasic,is,powerful"
delimiter = ","
tokenCount = PARSECOUNT(mainString, delimiter)
PRINT "Number of tokens: "; tokenCount
Summary of Differences
Purpose:
Tally: Counts the number of times a specific substring appears in a string.
ParseCount: Counts the number of tokens in a string based on a delimiter.
Usage:
Use Tally when you need to know how many times a particular substring appears in a string.
Use ParseCount when you need to know how many parts a string can be split into based on a delimiter.
Both functions are useful for string manipulation, but they are suited to different tasks.
Thanks Theo for feedback Its more clear now to understand the difference between Tally ( more Things to Count) and Parse( more Things to SEARCH for) I wasnt Sure about this topic so I am asking ..
Powerbasic example both command in one example
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
DIM mainString AS STRING
DIM delimiter AS STRING
DIM tokenCount AS LONG
mainString = "PowerBasic,is,very,powerful"
delimiter = ","
tokenCount = PARSECOUNT(mainString, delimiter )
MSGBOX "Number of tokens: " + STR$(tokenCount) ' result 4 words
tokenCount = TALLY(mainString, delimiter)
MSGBOX "Number of tokens2: " + STR$(tokenCount) ' 3 counts to ',' comma here
END FUNCTION