Supp- Str Search 2/2

 BBS: Inland Empire Archive
Date: 01-17-93 (01:17)             Number: 340
From: DIK COATES                   Refer#: NONE
  To: TOM CARROLL                   Recvd: NO  
Subj: Supp- Str Search 2/2           Conf: (2) Quik_Bas
>>>> QUOTING Dik Coates to Tom Carroll <<<<

Continued from previous message...

Copyright 1993 by R.A. Coates, free to use commercially with
acknowledgement, free for all other use.

FUNCTION SEARCHASC& (findstr$, mainstr$)
  searchchr% = ASC(LETFREQ$(findstr$, ptr%))
  strlen1% = LEN(mainstr$)
  strlen2% = LEN(findstr$)
  PRINT "Procedure SEARCHASC& Level Code"
  PRINT "*******************************"
  PRINT "The least frequent character to search for is:  "; searchchr%
  PRINT "The offset to the least frequent character is: "; ptr%
  PRINT "The Length of the string to search is: "; strlen1%
  optr% = ptr% - 1
  timer1 = TIMER
  DO
  LOOP WHILE timer1 = TIMER
  timer1 = TIMER
  IF ptr% THEN
    FOR c% = ptr% TO strlen1%
      chr1% = ASC(MID$(mainstr$, c%, 1))
      IF searchchr% = chr1% THEN
        IF MID$(mainstr$, c% - optr%, strlen2%) = findstr$ THEN
          SEARCHASC& = c% - optr%
          PRINT "Elapsed time using ASC is: "; TIMER - timer1
          PRINT
          EXIT FUNCTION
        END IF
      END IF
    NEXT c%
  END IF
  PRINT
END FUNCTION

FUNCTION SEARCHSTR& (findstr$, mainstr$)
  searchchr$ = LETFREQ$(findstr$, ptr%)
  strlen1% = LEN(mainstr$)
  strlen2% = LEN(findstr$)
  PRINT "Procedure SEARCHSTR& Level Code"
  PRINT "*******************************"
  PRINT "The least frequent character to search for is:  "; searchchr$
  PRINT "The offset to the least frequent character is: "; ptr%
  PRINT "The Length of the string to search is: "; strlen1%
  optr% = ptr% - 1
  timer1 = TIMER
  DO
  LOOP WHILE timer1 = TIMER
  timer1 = TIMER
  IF ptr% THEN
    FOR c% = ptr% TO strlen1%
      chr1$ = MID$(mainstr$, c%, 1)
      IF searchchr$ = chr1$ THEN
        IF MID$(mainstr$, c% - optr%, strlen2%) = findstr$ THEN
          SEARCHSTR& = c% - optr%
          PRINT "Elapsed time using CHR$ is: "; TIMER - timer1
          PRINT
          EXIT FUNCTION
        END IF
      END IF
    NEXT c%
  END IF
  PRINT
END FUNCTION

Using the ASC approach is about 15% faster than using CHR$ values.
You can tighten the code up if you want/can... The print statements
are for information only as part of the demo from the main body.
I haven't looked at string searches in BAS... and there may be a
faster method of looping through... maybe using INSTR is faster...
have routines in ASM as part of my library that I use... but have
only used a search a couple of times in the last 5-10 years...
Ho Hum...

Regards Dik

... WINDOZE...better than Valium and not habit forming!
___ Blue Wave/QWK v2.10

--- Maximus 2.00
 * Origin: Durham Systems (ONLINE!) (1:229/110)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET