My TALLY problem!
This one bit me hard today and all stems from my not reading the help file completely
I have used TALLY in many situations similar to the code below and did not realize until today that it will not always return what I had expected.
My BAD.
I did ask for a feature request to extend TALLY so when a match is found and the match string length is greater than 1 start the next match at the position of the prior match.
So in the slim chance there are other " I don't need no stinkin help file" morons, I submit MyTally.
James
This one bit me hard today and all stems from my not reading the help file completely
When a match is found, the scan for the next match begins at the position immediately following the prior match.
My BAD.
I did ask for a feature request to extend TALLY so when a match is found and the match string length is greater than 1 start the next match at the position of the prior match.
So in the slim chance there are other " I don't need no stinkin help file" morons, I submit MyTally.
James
Code:
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 'PBCC or PBWIN '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* #COMPILE EXE #DIM ALL #IF NOT %DEF(%WINAPI) #IF NOT %DEF(%WINDOWS) DECLARE FUNCTION GetStdHandle LIB "KERNEL32.DLL" ALIAS "GetStdHandle" (BYVAL nStdHandle AS DWORD) AS DWORD DECLARE FUNCTION AllocConsole LIB "KERNEL32.DLL" ALIAS "AllocConsole" () AS LONG %STD_OUTPUT_HANDLE = -11& %STD_INPUT_HANDLE = -10& DECLARE FUNCTION LoadLibrary LIB "KERNEL32.DLL" ALIAS "LoadLibraryA" (lpLibFileName AS ASCIIZ) AS LONG DECLARE FUNCTION GetProcAddress LIB "KERNEL32.DLL" ALIAS "GetProcAddress" (BYVAL hModule AS DWORD, lpProcName AS ASCIIZ) AS LONG DECLARE FUNCTION FreeLibrary LIB "KERNEL32.DLL" ALIAS "FreeLibrary" (BYVAL hLibModule AS DWORD) AS LONG #ENDIF #ENDIF '============================================================================== FUNCTION PBMAIN () AS LONG LOCAL S1 AS STRING LOCAL i AS LONG AllocConsole 'This has 3 "BJF1" but 2 are consecutive and TALLY does not pick it up S1 = ",B17A,B6,B26B,B26B,B21,B24,B26B,B3,B14A,B26A,B23A,B7A,B11A,B5A,B7B,B14A,B25,B20B" + _ ",B8,B22,B11A,B25,B17A,B12,BJF2,B15,B3B,B20A,BJF1,B20A,B23B,B11B,B13,B26A,B4A,BJF" + _ "2,B17B,B14B,BJF2,BJF1,BJF1,B17B,B11B,B1B,B10,B24,B20B,B1A,B23A,B18,B5B,B19,B16,B" + _ "1,B14B,B23B,B3A," i = TALLY(S1,",BJF1,") CPRINT "With Tally -> " +FORMAT$(i) i = MyTally(S1,",BJF1,") CPRINT "With MyTally -> " + FORMAT$(i) CWAITFORENTER END FUNCTION '============================================================================== FUNCTION MyTally(sMain AS STRING,sMatch AS STRING) AS LONG LOCAL j,k,n,MainLen,MatchLen AS LONG MatchLen = LEN(sMatch) MainLen = LEN(sMain) IF MatchLen = 0 OR MainLen = 0 THEN EXIT FUNCTION END IF n = 1 DO k = INSTR(n,sMain,sMatch) IF k THEN INCR j n = k+ MatchLen-1 IF n > MainLen THEN EXIT LOOP END IF ELSE EXIT LOOP END IF LOOP FUNCTION = j END FUNCTION '============================================================================== SUB CPRINT (BYVAL s AS STRING, OPT NoReturn AS LONG) LOCAL h,n AS LONG h = GetStdHandle(%STD_OUTPUT_HANDLE) n = FREEFILE OPEN HANDLE h FOR OUTPUT AS n IF ISMISSING(NoReturn) THEN PRINT# n, s ELSE PRINT# n, s; END IF CLOSE n END SUB '============================================================================== SUB CWAITFORENTER () LOCAL h,n AS LONG, S AS STRING h = GetStdHandle(%STD_INPUT_HANDLE) n = FREEFILE OPEN HANDLE h FOR INPUT AS n LINE INPUT# n, S CLOSE n END SUB
Comment