Paradox Community
Search:

 Welcome |  What is Paradox |  Paradox Folk |  Paradox Solutions |
 Interactive Paradox |  Paradox Programming |  Internet/Intranet Development |
 Support Options |  Classified Ads |  Wish List |  Submissions 


Paradox Programming Articles  |  Beyond Help Articles  |  Tips & Tricks Articles  


Beyond Help: breakApart
by Tony McGuire

Paradox Help on breakApart()

Splits a string into an array of substrings.

Syntax
breakApart ( var tokenArray Array[ ] String [ , const separators String ] )
Description

breakApart splits a string into an array of substrings and each substring is written to an element of an array named tokenArray. You can specify one or more delimiting characters in separators. If you omit separators, substrings are delimited by a space. Delimiting characters are not included in tokenArray. breakApart is especially useful for importing data from a text file into a table.

Note
  • Two empty delimiters parse as a token and result in an empty array element.

Beyond Help

breakApart is used to separate sections of a string into array elements. Why is this particularly noteworthy? As the help above indicates, importing a text file is one way that breakApart can be critical to your success.

Often, text fields contain quote characters. A comma delimited text file using quotes might look like:

"Corel","Paradox","Version 10","Quote of the Day: I Like it"

So using "value" (quote value quote) as a way to 'surround' text fields in an export process becomes meaningless when the quote character becomes part of one of the fields. This might look like:

"Corel","Paradox","Version 10","Quote of the Day: "I Like it""

In this case, the quotes must be abandoned as a means of surrounding field values, and a different value than comma must be used to separate fields; most often the tab character is used. This might look like:

Corel     Paradox     Version 10     Quote of the Day: "I Like it"

Often, though, a particular program or programmer doesn't (or can't) use tab to separate fields. In most cases, Paradox can handle these characters as part of its import routine. In those cases where Paradox can't handle the separator used in the text file, breakApart will save the day.

Another example is where the text file you need to 'import' into a table contains incorrectly formatted data. As an example, let's say that there is a field in the data that was intended as a date. But the information actually in the file also contains the time, not formatted as a recognizable datetime. You need to pull out the date when you place the data into your Paradox table.

By using breakApart to place each record into an array, you can then use Paradox's substr, match or breakApart functions to change the appropriate array item to the date format you need as part of your import process.

To put breakApart to use, we'll need to declare some variables:
var
  ; this tcursor will open the table that we
  ; want to 'import' the data into
  tcImport TCursor
  ; resizable array, we don't know the
  ; # of records in the text file ahead of time
  ; breakapart requires resizable array
  arRead Array[] String
  ; resizable array, we don't know the
  ; # of fields in each record ahead of time
  ; breakapart requires resizable array
  arBreak Array[] String
  ; resizable array, to use for breaking up
  ; fields that need additional operations applied
  ; breakapart requires resizable array
  arSubstr Array[] String
  ; for doing a 'findfirst' to guarantee the
  ; text file we'll be importing actually exists
  fsRead FileSystem
  ; used for opening and reading the
  ; text file into arBreak
  tsRead TextStream
  ; used for stepping through each array item
  liCounter LongInt
endVar

if not fsRead.findFirst("importfile.txt") then
  msgStop("Error","Can't find Import File")
  return
endIf

; open file to be 'imported' in read only mode
tsRead.open("importfile.txt","r")
; this operation reads the text file and places
; each line into an array element in arRead
tsRead.readLine(arRead)
; don't need access to the text file any more,
; so close it
tsRead.close()
Now, we have an array, arRead, whose elements each contain the information that comprises a record to be inserted into our table.

But how do we get a long string of data separated into the fields?

We'll step through each array element, break it up into a new array, perform any necessary operations on the field data, then use copyFromArray() to place the formatted data into our table.
; open pointer to 'import' table
tcImport.open("receive.db")
; prepare it to have records added
tcImport.edit()

; step through each array element
for liCounter from 1 to arRead.size()
  ; the fields in the text file are
  ; separated by the check mark
  arRead[liCounter].breakApart(arBreak,chr(251))
  ; our date field is the 15th field in the text file
  ; since we are breaking apart based on space,
  ; eliminate leading and trailing spaces
  arBreak[15]=lTrim(rTrim(arBreak[15]))
  ; the date value is separated from the
  ; time value by a space
  arBreak[15].breakApart(arSubstr," ")
  ; the date value is the second part of
  ; the text value (10:25Z 6/30/2001)
  ; I threw in arSubstr.size() for those instances
  ; where there is more than one space separating
  ; time from date in the field. 'Match' wouldn't work
  ; as in this instance for this very reason
  arBreak[15]=arSubstr[arSubstr.size()]
  ; create a new record to hold data
  tcImport.insertRecord()
  ; copy the new data from the array into the new record
  tcImport.copyFromArray(arBreak)
  ; 'save' the record
  tcImport.postRecord()
endFor

tcImport.endEdit()
tcImport.close()
Of course, this is a minor example of formatting during the 'import' process, using the date field. You can perform many operations once you have the individual field data in its own array element. You can even add blank array elements, perform calculations based on one or more of the other array elements, and store the resulting value in the new array element.

Another major use of the above process is when you have a text file that contains record information where one or more of the fields contain more than 255 characters. Paradox cannot import these files through the built-in import functions, neither interactively nor through ObjectPAL®. Its Alpha field type limit is 255 and it will only import the maximum of 255 characters regardless of the data in the file being imported. And it won't import a file at all when the destination table contains a memo field. In this case, the process above is the only way to bring the entire file into a Paradox table.


Discussion of this article


 Feedback |  Paradox Day |  Who Uses Paradox |  I Use Paradox |  Downloads 


 The information provided on this Web site is not in any way sponsored or endorsed by Corel Corporation.
 Paradox is a registered trademark of Corel Corporation.


 Modified: 15 May 2003
 Terms of Use / Legal Disclaimer


 Copyright © 2001- 2003 Paradox Community. All rights reserved. 
 Company and product names are trademarks or registered trademarks of their respective companies. 
 Authors hold the copyrights to their own works. Please contact the author of any article for details.