home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1993 #2
/
Image.iso
/
database
/
p4w_all.zip
/
TI1493.ASC
< prev
next >
Wrap
Text File
|
1993-05-12
|
7KB
|
265 lines
PRODUCT : Paradox for Windows NUMBER : 1493
VERSION : 1.0
OS : WIN
DATE : May 12, 1993 PAGE : 1/4
TITLE : Performing a Search and Replace of a Character
Intended Audience:
Beginning ObjectPAL users
Prerequisites:
Some familiarity with ObjectPAL is helpful
Purpose of the TI:
This document will illustrate how you can use ObjectPAL to search
for the occurrence of a specific character and replace it with
another character for each record in a table.
It is often desirable to search for every occurrence of a
specific character and replace it with another character for each
record in a table. For example, suppose you have a table named
"Test" that has an Alphanumeric field called ID with 3 records,
as shown below, and you want to replace all occurrences of a
comma (,) with periods (.).
TEST═╦════ID═════╗
║123,567 ║
║1,234,567 ║
║1,234 ║
With a small table like this, it is easy to modify each record
manually. But with a big table (e.g. over 1000 records), it is
not going to be very efficient. ObjectPAL automates the process.
On the following page is an example script which will perform the
search and replace quickly. The script performs a search and
replace on the "ID" field of the table "Test". Information on
how to create and run a script follows at the end of this
document.
PRODUCT : Paradox for Windows NUMBER : 1493
VERSION : 1.0
OS : WIN
DATE : May 12, 1993 PAGE : 2/4
TITLE : Performing a Search and Replace of a Character
EXAMPLE 1
method run(var eventInfo Event)
var
oldstring String
newstring String
tc TCursor
ar Array[] String
tv TableView
endVar
tc.open("TEST.DB") ;open TCursor for "TEST.DB"
tc.edit() ;begin edit mode
scan tc: ;start scan loop
newstring = "" ;Initialize newstring
;--pass the value of ID field to variable oldstring
;--use breakApart() to split oldstring to an array of
;--substrings
oldstring = tc."ID"
oldstring.breakApart(ar,",")
;--concatenate the array of substrings to newstring
for i from 1 to ar.size()
if i <> ar.size() ;replace comma with period
then newstring = newstring + ar[i] + "."
else newstring = newstring + ar[i]
endIf
endFor
;--pass the value of newstring to ID field
tc."ID" = newstring
endScan
tc.close() ;close TCursor
tv.open("TEST.DB") ;view the table "TEST.DB"
tv.bringToTop()
endmethod
NOTE: The semicolon ";" indicates that a comment follows. The
comment will not be processed when when you play the
script.
PRODUCT : Paradox for Windows NUMBER : 1493
VERSION : 1.0
OS : WIN
DATE : May 12, 1993 PAGE : 3/4
TITLE : Performing a Search and Replace of a Character
EXAMPLE 2
A variation on this script removes the "," entirely. The
modified code would be:
method run(var eventInfo Event)
var
oldstring String
newstring String
tc TCursor
ar Array[] String
tv TableView
endvar
tc.open("TEST.DB") ;open TCursor
tc.edit() ;begin edit mode
scan tc: ;start scan loop
newstring = "" ;Initialize newstring
;--pass the value of ID field to variable oldstring
;--use breakApart() to split oldstring to an array of
;--substring
oldstring = tc."ID"
oldstring.breakApart(ar,",")
;--concatenated the array of substrings to newstring
for i from 1 to ar.size()
newstring = newstring + ar[i]
endFor
;--pass the value of newstring to ID field
tc."ID" = newstring
endscan
tc.close() ;close TCursor
tv.open("TEST.DB") ;open the table "TEST.DB"
tv.bringToTop()
endmethod
To Create the Scripts Shown in Example 1 and Example 2:
Choose File | New | Script from the Desktop. Type the ObjectPAL
code that appears in either Example 1 or Example 2.
PRODUCT : Paradox for Windows NUMBER : 1493
VERSION : 1.0
OS : WIN
DATE : May 12, 1993 PAGE : 4/4
TITLE : Performing a Search and Replace of a Character
Check the Syntax
After you have typed in the script for either Example 1 or
Example 2, choose Language | Check Syntax from the Desktop. The
compiler examines the code and identifies if there are syntax
errors. It displays a message in the status line of the open
Editor window, such as "No syntax errors" or a description of the
syntax error. If there is a syntax error, Paradox positions the
cursor at the point of the first error. Before you can run the
script, you will need to correct your code and choose Language |
Check Syntax again until there are no remaining syntax errors.
When there are no syntax errors:
1. Choose File | Save from the Desktop, type in a filename in
the edit box, then choose OK.
2. To run the script, press [F8]. Your script will play, and
display your table in View mode.
Each of these examples demonstrate the reformatting of text using
basic ObjectPAL methods. Many text formatting problems can be
resolved using these same functions in similar methods.
Suggested Reading:
scan Chapter 3, ObjectPAL Reference manual
subStr Chapter 4, ObjectPAL Reference manual
breakApart Chapter 4, ObjectPAL Reference manual
TCursor Chapter 10, ObjectPAL Developer's Guide
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.