How Create an Array from .CSV file ?

Post Reply
shri_fwh
Posts: 301
Joined: Mon Dec 07, 2009 2:49 pm

How Create an Array from .CSV file ?

Post by shri_fwh »

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 19.12, BCC 7 32 bit, MariaDB
User avatar
ukoenig
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: How Create an Array from .CSV file ?

Post by ukoenig »

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
Image

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.
shri_fwh
Posts: 301
Joined: Mon Dec 07, 2009 2:49 pm

Re: How Create an Array from .CSV file ?

Post by shri_fwh »

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: Select all | Expand


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 19.12, BCC 7 32 bit, MariaDB
User avatar
ukoenig
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: How Create an Array from .CSV file ?

Post by ukoenig »

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: Select all | Expand


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.
shri_fwh
Posts: 301
Joined: Mon Dec 07, 2009 2:49 pm

Re: How Create an Array from .CSV file ?

Post by shri_fwh »

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 19.12, BCC 7 32 bit, MariaDB
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: How Create an Array from .CSV file ?

Post by nageswaragunupudi »

Code: Select all | Expand

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.

Image
Regards

G. N. Rao.
Hyderabad, India
shri_fwh
Posts: 301
Joined: Mon Dec 07, 2009 2:49 pm

Re: How Create an Array from .CSV file ?

Post by shri_fwh »

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: Select all | Expand


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 19.12, BCC 7 32 bit, MariaDB
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: How Create an Array from .CSV file ?

Post by nageswaragunupudi »

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: Select all | Expand


cCsv := MEMOREAD( cCsvFile )
 
Regards

G. N. Rao.
Hyderabad, India
shri_fwh
Posts: 301
Joined: Mon Dec 07, 2009 2:49 pm

Re: How Create an Array from .CSV file ?

Post by shri_fwh »

Dear Rao Sir ,

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


Thanks
Shridhar
Thanks
Shridhar
FWH 19.12, BCC 7 32 bit, MariaDB
Post Reply