Lists versus arrays (when working with .csv)

refsmmat

Member
Licensed User
Longtime User
Hello,

im new to B4A & have not previously used VB.

I would like to read/interpret a .CSV file (3 columns, nominally 500 rows). I have successfully imported it as a list, but I am having some trouble extracting data from the list. I would like to do so in the way typically used for arrays (in other languages)

I.E. value = my_array[x,y]

I am wondering if a list can be a 2D entity as I have only seen example code addressing 1 axis of a list.

Is it the case that my list import

Dim lst As List
lst = su.LoadCSV(File.DirRootExternal, "locations1.csv", ",")

has created a one dimensional array?

If so, could someone pass me a recommendation to imprt a .csv to a 2D array?
Alternatively a recommendation of appropriate ref material would be useful.
I looked at the scrollview example, but I couldnt follow how the data was broken out from a list to a 2D data set .

Many thanks.
refsmmat
 

sarkis

Member
Licensed User
Longtime User
I did something like this with txt files

ListAll=File.ReadList( .....)
Dim str as string

For i =0 to <History.Size -1
str=ListAll.Get(i)
...
Next
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
Your code gives you a List of String Arrays - each Array will have 3 elements.

You could then create an Array from that List:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim lst As List
   Dim i, j, ColCount, RowCount As Int
   Dim su As StringUtils
   lst = su.LoadCSV(File.DirAssets, "myvalues.csv", ",")
   RowCount=lst.Size
   ColCount=11
   Dim MyArray(RowCount, ColCount), aRow() As String
   For i=0 To RowCount-1
      aRow=lst.Get(i)
      For j=0 To ColCount-1
         MyArray(i, j)=aRow(j)
      Next
   Next
   Log(MyArray(6, 0))
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

In this example myvalues.csv has 7 rows each with 11 columns.

Martin.
 

Attachments

  • ListToArray.zip
    5.9 KB · Views: 269
Upvote 0

refsmmat

Member
Licensed User
Longtime User
Many thanks Sarkis & Warwound.
The example code you have submitted is excellent & makes the list issue (& resolution) very easy for me to undestand.
Best wishes,
Refsmmat
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…