Jens �ge Nielsen wrote:
> I want MapBasic to read an ASCII text file and search for a particular text
> string. If the string is found, it should be replaced by another string. The
> new string may be shorter or longer than the original one. The text file is
> sequential, not fixed-length.
There's more than one way to do this, but here's a possible
solution, assuming the following conditions are true. 1. Your ASCII
file contains individual lines of text, each line terminated by a
newline or carriage return character. 2. No line of text in your
file is longer than 255 characters.
If these conditions are met, then open the text file for input and
use "line input" to read in each line. Open another for output. Then
read each character in the string, looking for the first one that
matches the first character in your target string. If you find a
match, keep track of the position in the string where it, and copy
the searched string up to that point to the output file. Check the
next characters to see if you really have found your string. If any
character is found that indicates that you haven't got the full
search substring, then copy everything from where you first started
checking up to the current point, and continue on. If you do find
your search string then simply copy the replacement string to the
output file instead, and proceed as before from where your search
string ends.
Do this line by line until the file is done, then close both files,
use "kill" to delete the original and "rename" to change the new
output file name to the original file name and you're done.
An obvious wrinkle in this algorithm is what happens if the search
string spans more than one line? If you are finding matches for your
search string, and you run out of line, then read another line and
tack it on to the first, and continue searching as you had been. You
might want to stick a newline (chr$(10)) in your output to sort of
maintain line wrapping but that's up to you. Actually, line wrapping
is really a separate function with its own bucket of worms to sort
out.
This is probably not the most efficient technique, but it's easy,
and it will work. A character by character file search in MB is
slow. You might speed it up by breaking out words instead of
characters with InStr() by searching for spaces as delimiters, and
then doing word string comparisons instead of character comparisons.
Actually, search and replace is a fairly knotty problem when you
really get into it, but it's an interesting problem. If you want
some real fun, try to build an editor in mapbasic. One that
supported RTF and HTML might be quite a handy tool, but I think if
you try this with MapBasic, you would find the job to be something
like building the Three Gorges Dam with a teaspoon.
- Bill Thoen
------------------------------------------------------------
GISnet, 1401 Walnut St., Suite C, Boulder, CO 80302
tel: 303-786-9961, fax: 303-443-4856
mailto:[EMAIL PROTECTED], http://www.ctmap.com/gisnet
------------------------------------------------------------
_______________________________________________________________________
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, send e-mail to [EMAIL PROTECTED] and
put "unsubscribe MapInfo-L" in the message body.