B4J Question su.SaveCSV array of string

rodmcm

Active Member
Licensed User
According the reading I have done if I save an array of string to a list and then that list to CSV the string array should be aligned in successive columns in a row
I have tried lots of variations on the attached, and all I can get is one column named "Name" in the row where there should be four values
Can someone assist please

B4X:
Sub btnSaveRunData_Click
    ' Select Directory
    Dim DC As DirectoryChooser                           
    DC.Initialize
    DC.InitialDirectory = "C:"
    Dim Folder As String = DC.Show(MainForm)
    
    ' Get Time and Day
    DateTime.TimeFormat="HH:mm"                         
    Dim TimeMin As String = DateTime.Time(DateTime.Now)
    DateTime.DateFormat = "dd MMM yyyy"
    Dim TimeDay As String = DateTime.Date(DateTime.now)
    Dim FileName As String = "NewData.csv"
    
    ' Add data row by row
    Dim su As StringUtils
    Dim List1 As List
    List1.Initialize
    List1.add(Array As String(TimeDay))   
    List1.add(Array As String(TimeMin))
    List1.add(Array As String(EngineType))
    List1.add(Array As String(EngineIdent1))
    List1.add(Array As String(EngineIdent2))
    List1.add(Array As String(EngineIdent3))
    
    '' add headers for new data
    Dim B(4) As String                               ' Row header for next lot of data
    B(0) = "Name"
    B(1) = "CC"
    B(2) = "Max Power"
    B(3) = "Max Torque"
    List1.add(B)
    For i = 0 To 99
    'more line data here added to list
    Next
    su.SaveCSV(Folder, FileName, ",",List1)
End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    8.3 KB · Views: 172

klaus

Expert
Licensed User
Longtime User
The problem are your first 6 Line1.Add lines.
It seems that the length of the first Array in the List is considered as the number of columns.

I tested the code below and this one works.

B4X:
Sub Test
    ' Select Directory
    Dim DC As DirectoryChooser
    DC.Initialize
    DC.InitialDirectory = "C:"
    Dim Folder As String = DC.Show(MainForm)
    
    ' Get Time and Day
    DateTime.TimeFormat="HH:mm"
    Dim TimeMin As String = DateTime.Time(DateTime.Now)
    DateTime.DateFormat = "dd MMM yyyy"
    Dim TimeDay As String = DateTime.Date(DateTime.now)
    Dim FileName As String = "NewData.csv"
    Dim EngineType As String = "aaa"
    Dim EngineIdent1 As String = "bbb"
    Dim EngineIdent2 As String = "ccc"
    Dim EngineIdent3 As String = "ddd"
    ' Add data row by row
    Dim su As StringUtils
    Dim List1 As List
    List1.Initialize
'    List1.add(Array As String(TimeDay))
'    List1.add(Array As String(TimeMin))
'    List1.add(Array As String(EngineType))
'    List1.add(Array As String(EngineIdent1))
'    List1.add(Array As String(EngineIdent2))
'    List1.add(Array As String(EngineIdent3))
    
    '' add headers for new data
    Dim B(4) As String                               ' Row header for next lot of data
    B(0) = "Name"
    B(1) = "CC"
    B(2) = "Max Power"
    B(3) = "Max Torque"
    List1.add(B)
    For i = 0 To 9
        Dim B(4) As String                               ' Row header for next lot of data
        For j = 0 To 3
            B(j) = "r" &i & " / c" & j
            List1.add(B)
        Next
    Next
    su.SaveCSV(Folder, FileName, ";",List1)
End Sub

I used ";" as the separator character to read directly with Excel.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
This might help ...
B4X:
Private Sub Button2_MouseClicked (EventData As MouseEvent)

    ' Select Directory
    Dim DC As DirectoryChooser                          
    DC.Initialize
    DC.InitialDirectory = "C:"
    Dim Folder As String = DC.Show(frmMain)
    Dim FileName As String = "NewData.csv"
   
    ' Get Time and Day
    DateTime.TimeFormat="HH:mm"                        
    Dim TimeMin As String = DateTime.Time(DateTime.Now)
    DateTime.DateFormat = "dd MMM yyyy"
    Dim TimeDay As String = DateTime.Date(DateTime.now)
   
     ' Add data row by row
    Dim su As StringUtils
    Dim List1 As List
    List1.Initialize
   
    Dim Headers(6) As String   = Array As String  ("Date", "Time", "Name", "CC", "Max Power", "Max Torque")  ' Row header for all data ?
    List1.Add(Headers)
   
    For i = 1 To 20
          List1.add(Array As String(TimeDay, TimeMin, "Honda #" & i, 100 * i, 2000 +(20 *i), 3000+i))
    Next
   
    su.SaveCSV(Folder, FileName, ",",List1)   
   
End Sub

Edit: Klaus beat me to it...
 
Upvote 0

rodmcm

Active Member
Licensed User
So I tried this, which didn't work either
Note how the second column has a piece of the first save "Ran"

If I use another array say C(4) for the second set of data and list1.add(C) then it works
So it seems that the string array has to be reset, which is what Klaus has done in the 9 additions



B4X:
Sub SaveData
    Dim DC As DirectoryChooser
    DC.Initialize
    DC.InitialDirectory = "C:"
    Dim Folder As String = DC.Show(MainForm)

    Dim FileName As String = "Test.csv"
   
    Dim su As StringUtils
    Dim List1 As List
    List1.Initialize
    Dim B(4) As String
    B(0) = "Rod"
    B(1) = "Ran"
    B(2) = "This"
    B(3) = "Today"
    List1.add(B)
    B(0) = "Name"
    B(2) = "CC"
    B(3) = "Max Power"
    B(0) = "Max Torque"
    List1.add(B)
    For i = 0 To 9
        Dim B(4) As String                               ' Row header for next lot of data
        For j = 0 To 3
            B(j) = "r" &i & " / c" & j
        Next
        List1.add(B)
    Next
    su.SaveCSV(Folder, FileName, ",",List1)
End Sub
 
Upvote 0

rodmcm

Active Member
Licensed User
Sorry
this is the code and the result... The above one has a mistake in it
You can see that the first set of data is ignored


B4X:
Sub SaveData
    Dim DC As DirectoryChooser
    DC.Initialize
    DC.InitialDirectory = "C:"
    Dim Folder As String = DC.Show(MainForm)

    Dim FileName As String = "Test.csv"
    
    Dim su As StringUtils
    Dim List1 As List
    List1.Initialize
    Dim B(4) As String
    B(0) = "Rod"
    B(1) = "Ran"
    B(2) = "This"
    B(3) = "Today"
    List1.add(B)
    B(0) = "Name"
    B(1) = "CC"
    B(2) = "Max Power"
    B(3) = "Max Torque"
    List1.add(B)
    For i = 0 To 9
        Dim B(4) As String                               ' Row header for next lot of data
        For j = 0 To 3
            B(j) = "r" &i & " / c" & j
        Next
        List1.add(B)
    Next
    su.SaveCSV(Folder, FileName, ",",List1)
End Sub
 

Attachments

  • Capture3.PNG
    Capture3.PNG
    18.7 KB · Views: 159
Upvote 0

rodmcm

Active Member
Licensed User
and again
If I use another array say C(4) for the second set of data and list1.add(C) then it works
So it seems that the string array has to be reset, which is what Klaus has done in the 9 additions
 
Upvote 0
Top