Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import TAB delimited files 1

Status
Not open for further replies.

Terenas

IS-IT--Management
May 28, 2001
11
CL
Hi all,
I need to import a TAB delimited file into a Clipper 5.3 DBF. The file has no field names on the first row and has no " delimiting each field.
The command APPEND FROM xxx DELIMITED WITH CHR(9) does not work as it imports the whole line into the first DBF field including all the TAB characters.
The file has the following structure:

AAA{tab}BBB{tab}CCC{tab}DDD{tab}
AAA{tab}BBB{tab}CCC{tab}DDD{tab}
AAA{tab}BBB{tab}CCC{tab}DDD{tab}

where {tab} is a CHR(9)

Thanks for all the help!

Sérgio Terenas
 
I've attached a neat little function which will take any string which contains a list of values delimited by any character you choose and will convert it to an array. For you to make use of this, you have 2 options. One, make use of your database with the long fields, or alternatively write a little routine to read the file in one line at a time. Either way, you'll have one line to pass to ListToArray, eg...

ListToArray(cMyVeryLongStringList, Chr(9)).

Once you have your array you can then dump it to the correct destination database using your favourite scatter (or is it gather?) function.

Have fun with it, Ya Pal,
KMC







/*----------------------------------------------------------------------------
Function: ListToArray
------------------------------------------------------------------------------
Purpose: Convert a delimited string to an array
------------------------------------------------------------------------------
Parms: (1) The string to be parsed (may contain several delimiters)
(2) The delimiter character (defaults to ",")
(3) An option string, indicating how each element is trimmed etc
N - No trimming of elements (by default, blanks are trimmed)
D - If string is empty, returns {""} i.e. an array with one
dummy element. Default is to return an empty array {}.
------------------------------------------------------------------------------
Returns: An array, with each element containing a segment of the string
------------------------------------------------------------------------------
Notes: Examples of usage:
aRes := ListToArray("AB, BC,CD ") // Result: {"AB","BC","CD"}
aRes := ListToArray("A, B, C ",,"N") // Result: {"A"," B"," C "}
aRes := ListToArray("A; B; C",";") // Result: {"A","B","C"}
aRes := ListToArray(" ") // Result: {}
aRes := ListToArray(" ","D") // Result: {""}
----------------------------------------------------------------------------*/

FUNCTION ListToArray ( cList, cDelimiter, cOptions )

local nPos
local aList := {} // Define an empty array
local cToken // Token in the list
local lMore := .T.
local lTrim := .T.
local nDelimLen

DEFAULT ( cDelimiter, "," )
nDelimLen := Len(cDelimiter)

if .not.Empty(cOptions) // Deal with the Options (if any supplied)
cOptions := Upper(cOptions)
lTrim := .not.("N" $ cOptions)
if Empty(cList) .and. .not.("D"$ cOptions) // Unless D, return an empty
lMore := .F. // array if string is blank.
endif
endif

do while lMore

nPos := at(cDelimiter, cList)

if nPos > 0 // If found a delimiter
cToken := substr(cList,1,nPos-1) // Extract token
else
cToken := cList // Else use remainder of str
lMore := .F.
endif

if lTrim
cToken := AllTrim(cToken) // Trim unless asked not to
endif

aAdd ( aList, cToken ) // Add a new element
cList := substr(cList, nPos+nDelimLen)

enddo

RETURN aList // End of function ListToArray
 
Thanks KMC,

I've just tested the function and it works perfect. Exactly what I was after. I'll keep reading new threads just in case I can provide help next time. :)

Thanks,

Sergio Terenas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top