TALLY function

Purpose:

TALLY returns the number of times Match occurs in MainStr

Syntax:

RetVal = TALLY(MainStr AS STRING, Match AS STRING [,CaseSensitivity AS INTEGER])

Return Value:

  • Data type: INTEGER
    RetVal count of how many times Match occurs in MainStr

Parameters:

  • Data type: STRING
    MainStr String to be searched for Match.
  • Data type: STRING
    Match Substring to be located in MainStr.
  • Data type: INTEGER
    CaseSensitivity [OPTIONAL] Set to 1 for case insensitive search. Default is 0 which is case sensitive.

Example 1:

This snippet uses the TALLY function default case sensitive mode.

DIM a$
DIM b$
DIM int1%

a$ = " ABC abc AbC aBc "
b$ = "abc"

int1% = TALLY(a$, b$)
PRINT int1%

Result:

1

Example 2:

This snippet uses the TALLY function optional case insensitive mode.

DIM a$
DIM b$
DIM int1%

a$ = " ABC abc AbC aBc "
b$ = "abc"

int1% = TALLY(a$, b$, 1)
PRINT int1%

Result:

4

Example 3:

This example uses the TALLY function to count linefeed characters from a buffered file.


PRINT FileLineCount("bc.bas")

FUNCTION FileLineCount (Filename$) AS LONG
  LOCAL FF AS FILE     ' local file name that we'll use 
  LOCAL Linefeeds      ' accumulate the line feeds in the text file 
  LOCAL Buff$ * 16384  ' 16k reusable buffer 
 
  IF EXIST(Filename$) = 0 THEN EXIT FUNCTION

  OPEN Filename$ FOR BINARY AS FF

  WHILE NOT EOF(FF)
    GET$ FF, Buff$, 16384
    Linefeeds += TALLY(Buff$, LF$)
    memset(Buff$, 0, 16384)
  WEND

  CLOSE FF
  FUNCTION = Linefeeds
END FUNCTION