ASCII TEXT SEARCH +

 BBS: Inland Empire Archive
Date: 03-31-93 (12:00)             Number: 334
From: SEAN SULLIVAN                Refer#: NONE
  To: JANUSZ SUCHOROLSKI            Recvd: NO  
Subj: ASCII TEXT SEARCH +            Conf: (2) Quik_Bas
Greetings and Salutations JANUSZ!

Monday March 29 1993 11:12, JANUSZ SUCHOROLSKI wrote to ALL:

 JS> I'd like to implement a "text search" while inside ASCII file.
 JS> Here's some code:

.....

 JS> It simply shows the whole line in reverse video, however I'd like to see
 JS> just the occurence(s) of the string highlighted instead of the whole
 JS> line. I guess somebody did it before and might post a missing
 JS> bit(bytes).

Janusz, I've reworked your code a little to give you what
you want.  Hope this helps.

---------------------8<-------cut here---------->8----------------------------

DEFINT A-Z
CLS

'Get input file and search string
LOCATE 1, 1: COLOR 0, 11: PRINT SPACE$(80)
LOCATE 1, 1: INPUT "Text file name: ", tf$
LOCATE 1, 1: PRINT SPACE$(80)
LOCATE 1, 1: INPUT "Search for:", ts$
ts$ = UCASE$(ts$)

'search string length
tl = LEN(ts$)
COLOR 7, 0

'open file
freenum = FREEFILE
OPEN tf$ FOR INPUT AS #freenum
LineNum = 0

'set up view port for printing file
VIEW PRINT 2 TO 24

'read in file line by line highlighting found search text found
WHILE NOT EOF(freenum)
  LineNum = LineNum + 1
  LINE INPUT #freenum, Check$

  'if line contains the search text, highlight that text
  IF INSTR(UCASE$(Check$), ts$) THEN
    TextFound = INSTR(UCASE$(Check$), ts$)
    COLOR 7, 0: Found$ = MID$(Check$, 1, TextFound - 1): PRINT Found$;
    COLOR 0, 7: Found$ = MID$(Check$, TextFound, tl): PRINT Found$;
    COLOR 7, 0: Found$ = MID$(Check$, TextFound + tl): PRINT Found$
  'else print the line
  ELSE PRINT Check$
  END IF

  'pause when page is full
  IF LineNum = 21 THEN
    COLOR 0, 12: PRINT "< MORE >": COLOR 7, 0
    DO
      Ky$ = INKEY$
    LOOP WHILE Ky$ = ""
    LineNum = 0
    CLS 2
  END IF

WEND

CLOSE #freenum

--------------------------8<-----cut here----->8-------------------------

I've added a top line prompt for easier testing and print the text in a
view port.  You were using INPUT to get the file data, but that chops up the
input if commas, etc, are in the line.  Using LINE INPUT
you get the whole line
at once.  If the string is found in the line, the line is then parsed to
highlight just the search string, otherwise the whole line
is printed as is.  I add a routine to pause when the screen
is full.  Hope this helps ya.

Sean

--- GoldED 2.40.P0623
 * Origin: COMNET Point #28 [Watervliet, NY] (1:267/113.28)
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