FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How Create an Array from .CSV file ?
Posts: 336
Joined: Mon Dec 07, 2009 02:49 PM
How Create an Array from .CSV file ?
Posted: Sun Aug 04, 2019 06:19 PM

Dear All ,

I want to create an array from .CSV file. any Function / Class for this ?

Please provide some sample code. Thanks in advance...!

Thanks
Shridhar

Thanks

Shridhar

FWH 24.04, BCC 7 32 bit, MariaDB
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: How Create an Array from .CSV file ?
Posted: Mon Aug 05, 2019 07:23 AM
Shridhar,

can You show some lines from the top that we know the structure
A solution can be to create a DBF from the CSV-file and next using -> DBFTOARRAY

About some infos working with CSV
viewtopic.php?f=3&t=25043&p=155953&hilit=dbfcsv#p155953

About DBFTOARRAY
viewtopic.php?f=3&t=36678&p=218808&hilit=dbftoarray#p218808

A CSV - structure


regards
Uwe :-)
Since 1995 ( the first release of FW 1.9 )

i work with FW.

If you have any questions about special functions, maybe i can help.
Posts: 336
Joined: Mon Dec 07, 2009 02:49 PM
Re: How Create an Array from .CSV file ?
Posted: Mon Aug 05, 2019 10:47 AM
Hi Uwe ,

the .CSV file format will be similar as given below, it is simply invoice data. it has three segments 1) header 2) detail 3) footer.

Code (fw): Select all Collapse
H,ABC CO.,08052019, INV00031
D,10210,CROCIN TAB , 10, 30.00, 300, 10.00,10.00,320.00
D,21205,AMLOX TAB  , 10, 20.00, 300, 10.00,10.00,220.00
D,21535,CFLOX TAB  , 10, 10.00, 300, 10.00,10.00,120.00
F,30,30,660.00


Thanks
Shridhar
Thanks

Shridhar

FWH 24.04, BCC 7 32 bit, MariaDB
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: How Create an Array from .CSV file ?
Posted: Mon Aug 05, 2019 11:29 AM
Does it mean that You need a array from the data-area ?
because a defined header and footer is not a normal CSV-file-structure
created from a DBF

Code (fw): Select all Collapse
aData := { { 10210,CROCIN TAB , 10, 30.00, 300, 10.00,10.00,320.00 }, ;
           { 21205,AMLOX TAB  , 10, 20.00, 300, 10.00,10.00,220.00 }, ;
           { 21535,CFLOX TAB  , 10, 10.00, 300, 10.00,10.00,120.00 } }


regards
Uwe :-)
Since 1995 ( the first release of FW 1.9 )

i work with FW.

If you have any questions about special functions, maybe i can help.
Posts: 336
Joined: Mon Dec 07, 2009 02:49 PM
Re: How Create an Array from .CSV file ?
Posted: Mon Aug 05, 2019 01:43 PM

Hi Uwe ,

Need 3 different arrays to process invoice data into our database.

Looking for a function which returns an array (single dim) for each record during .CSV line read then I can copy /add record into relevant main array

Thanks
shridhar

Thanks

Shridhar

FWH 24.04, BCC 7 32 bit, MariaDB
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How Create an Array from .CSV file ?
Posted: Thu Aug 08, 2019 12:58 AM
Code (fw): Select all Collapse
aData := HB_ATokens( cCsv, CRLF )
AEval( aData, { |c,i| aData[ i ] := HB_ATokens( c, "," ) } )


All elements of the array are untrimmed character values. You need to convert them into the required data type yourself.

Regards



G. N. Rao.

Hyderabad, India
Posts: 336
Joined: Mon Dec 07, 2009 02:49 PM
Re: How Create an Array from .CSV file ?
Posted: Thu Aug 08, 2019 05:44 AM
Dear Rao Sir ,

Simple and Excellent solution ...! Thanks a lot ...!

Just one question on the variable cCsv ,

Shall I use below method to read entire .CSV file and copy data into cCsv variable or any other function/logic ? please guide for the same.

Code (fw): Select all Collapse
local cCSVFile := "purcbill.csv"
local cCsv 

oFile  := TTxtFile():New( cCSVFile )
oFile:GoTop()
Do while !oFile:Eof() 
    cCsv := cCsv  + oFile:ReadLine()
    oFile:Skip()
Enddo


aData := HB_ATokens( cCsv, CRLF )
AEval( aData, { |c,i| aData[ i ] := HB_ATokens( c, "," ) } )


Thanks
Shridhar
Thanks

Shridhar

FWH 24.04, BCC 7 32 bit, MariaDB
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How Create an Array from .CSV file ?
Posted: Thu Aug 08, 2019 06:44 AM
local cCSVFile := "purcbill.csv"
local cCsv

oFile := TTxtFile():New( cCSVFile )
oFile:GoTop()
Do while !oFile:Eof()
cCsv := cCsv + oFile:ReadLine()
oFile:Skip()
Enddo


Instead of the above six lines of code,
Code (fw): Select all Collapse
cCsv := MEMOREAD( cCsvFile )
Regards



G. N. Rao.

Hyderabad, India
Posts: 336
Joined: Mon Dec 07, 2009 02:49 PM
Re: How Create an Array from .CSV file ?
Posted: Thu Aug 08, 2019 07:58 AM

Dear Rao Sir ,

Supper..! Thanks a lot once again...!

Thanks
Shridhar

Thanks

Shridhar

FWH 24.04, BCC 7 32 bit, MariaDB

Continue the discussion