APPEND FROM <text-file> DELIMITED WITH ";"
AAAA;BBBB;CCCC;DDDD;EEEE // Text-Header
111;2;33;4;5555 // values 1. Line
11;333;5;88;7 // values 2. Line
The DBF at the end must look like :
AAAA BBBB CCCC DDDD EEEE // Fieldnames
111 2 33 4 5555 // Values
11 333 5 88 7
FUNCTION PARSESTR(cText, cDelim)
/*
Parses a string into substrings
wherever <cDelim> is found and returns an
array of these substrings.
An empty string will result in an empty array.
<cDelim> defaults to a comma.
Example: PARSESTR("AAA;BBB;CCC") => { "AAA", "BBB", "CCC" }
*/
local aTemp_ := {}
local cWork, n := 1
default cDelim := ","
if !empty(cText)
cWork := cText
do while n > 0
n := at(cDelim, cWork)
if n > 0
aadd(aTemp_, left(cWork, n - 1))
cWork := substr(cWork, n + len(cDelim))
else
aadd(aTemp_, cWork)
endif
enddo
endif
Return(aTemp_)
#include 'fivewin.ch'
#include 'xbrowse.ch'
function Main()
local cBuf
local aData, aHeaders
cBuf := MemoRead( 'input.txt' )
// parse and make a multi dimentional array
if cBuf[ -1 ] == 26 // check and remove Ctrl-Z
cBuf := Left( cBuf, Len( cBuf ) - 1 )
endif
if Right( cBuf, 2 ) != CRLF
// pad with CRLF if needed, not to miss the last line
cBuf += CRLF
endif
cBuf := StrTran( cBuf, CRLF, Chr( 10 ) )
aData := hb_aTokens( cBuf, Chr(10) )
AEval( aData, { |c,i| aData[ i ] := hb_aTokens( c, ';' ) } )
aHeaders := aData[ 1 ]
aData := ADel( aData, 1 )
aSize( aData, Len( aData ) - 1 )
// Parsing is done
// now view the data quickly in xbrowse before writing in DBF
xBrowse( aData, ; // data to browse
"Parsed Data as Array", ; // title
.f., ; // no autosort
{ |oBrw| AEval( oBrw:aCols, { |o,i| o:cHeader := aHeaders[ i ] } ) } ;
) // codeblock above assigns header names
return nil
#include 'fivewin.ch'
#include 'xbrowse.ch'
function Main()
local cBuf
local aData, aHeaders
cBuf := MemoRead( 'input.txt' )
// parse and make a multi dimentional array
if cBuf[ -1 ] == 26 // check and remove Ctrl-Z
cBuf := Left( cBuf, Len( cBuf ) - 1 )
endif
if Right( cBuf, 2 ) != CRLF
// pad with CRLF if needed, not to miss the last line
cBuf += CRLF
endif
cBuf := StrTran( cBuf, CRLF, Chr( 10 ) )
aData := hb_aTokens( cBuf, Chr(10) )
AEval( aData, { |c,i| aData[ i ] := hb_aTokens( c, ';' ) } )
aHeaders := aData[ 1 ]
aData := ADel( aData, 1 )
aSize( aData, Len( aData ) - 1 )
// Parsing is done
// now view the data quickly in xbrowse before writing in DBF
xBrowse( aData, ; // data to browse
"Parsed Data as Array", ; // title
.f., ; // no autosort
{ |oBrw| AEval( oBrw:aCols, { |o,i| o:cHeader := aHeaders[ i ] } ) } ;
) // codeblock above assigns header names
// Extending the program to Write DBF
WriteToDBF( 'INPUT.DBF', aData, aHeaders )
// Check the DBF
USE INPUT
XBrowse()
return nil
static function WriteToDBF( cDbf, aData, aHeaders )
local aStruct := {}
local n, nLen := Len( aData )
AEval( aHeaders, ;
{ |c| AAdd( aStruct, ;
{ Upper( Left( Trim( c ), 10 ) ), 'C', 2, 0 } );
} )
// desirable to remove embedded chars not acceptable in fieldnames
// and to check for duplicates
// that logic is not included here
for n := 1 to nLen
AEval( aData[ n ], ;
{ |c,i| c := Trim( c ), ;
aStruct[ i ][ 3 ] := Max( Len( c ), aStruct[ i ][ 3 ] ), ;
aData[ n ][ i ] := c ;
} )
next n
dbCreate( cDbf, aStruct )
USE ( cDbf ) NEW ALIAS OUT EXCLUSIVE
for n := 1 to nLen
OUT->( dbAppend() )
AEval( aData[ n ], { |c,i| OUT->( FieldPut( i, c ) ) } )
next
OUT->( dbCloseArea() )
return nil
#include "FiveWin.ch"
function Main()
local cText := "C813354668;1063567698;0160 / 1853735;E+;NX;07.07.08;18:58:11;01732644433;VF;;00:28;0;0,0823;0,0000;NX"
local n, cResult := ""
for n = 1 to 15
cResult += StrToken( cText, n, ";" ) + CRLF
next
MsgInfo( cResult )
return nil
#include "FiveWin.ch"
function Main()
local cText := "C813354668;1063567698;0160 / 1853735;E+;NX;07.07.08;18:58:11;01732644433;VF;;00:28;0;0,0823;0,0000;NX"
local n, cResult := "", nTimes := StrCharCount( cText, ";" ) + 1
for n = 1 to nTimes
cResult += StrToken( cText, n, ";" ) + CRLF
next
MsgInfo( cResult )
return nil
PS: O.T. I use so many times for checking if a variable is empty
this code:
if len(ALLTRIM(cText) = 0
Is there a build in function existing?
if empty(cText)
...
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot], nageswaragunupudi and 110 guests