SaveCSV with a list of arrays

HarleyM

Member
Licensed User
Longtime User
Hi Guys,

I've been trying to get saveCSV to work with a list of arrays & all i seem to get is the last array repeated for each of the items in the list... I've shortened my code down to the following in an attempt to get it working...
(I'm having to retype this as the program cant be ported to this PC - foregive typos cheers!)

MyFile & MyFolder defined elsewhere work as required
Dim Savelist as list
Dim export as stringutils
Dim Headers(9)
Dim row(9)
Dim x,y as int
Savelist.initialize
Headers= Array as string("col1",col2"....,"col9")

SaveList.clear
For y = 0 to 10
for x = 0 to 8
row(x) = y & "-" & x
next
savelist.add(row)
next

export.saveCSV2(MyFolder,MyFile & ".csv",",",Savelist,Headers)



The file is written but the contents are something like :-

Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8
10-0,10-1,10-2,10,3,10-4,10-5,10-6,10-7,10-8

I was hoping for :-
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9
0-0,0-1,0-2,0-3,0-4,0-5,0-6,0-7,0-8
1-0,1-1,1-2,1-3,1-4,1-5,1-6,1-7,1-8
2-0,2-1,2-2,2-3,2-4,2-5,2-6,2-7,2-8
3-0,3-1,3-2,3-3,3-4,3-5,3-6,3-7,3-8
.....
10-0,10-1,10-2,10-3,10-4,10-5,10-6,10-7,10-8

Where am I going wrong??? :sign0085:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Arrays (and other objects as well) are not copied when you pass them to other methods. A reference is passed instead.
In your case you are adding the same array object to the list many times.
You should create a new array for each row:
B4X:
For y = 0 to 10
Dim Row(9) As String 'Create a new array
     for x =  0 to 8
          row(x) = y & "-" & x
     next
     savelist.add(row)
next
 
Upvote 0

HarleyM

Member
Licensed User
Longtime User
Cheers Erel. The more I do, the more I realize I don't know!
 
Upvote 0
Top