CSV file to array help

tonytoy

New Member
Licensed User
Longtime User
I have a csv file that i need to read into an array so I can use the data. I'm not finding how to do this.
I saw how to read the csv into a string, what i need is how to parse the string to make the array or linked list.

If you have any help or examples please let me know.

:sign0089:
 

Attachments

  • stops example.txt
    15.2 KB · Views: 448

coslad

Well-Known Member
Licensed User
Longtime User
Hi

I tried to load the file that TonyToy posted as example : "stops example.txt"

My code is :


Dim su As StringUtils
Dim list1 As List
list1 = su.LoadCSV(File.DirAssets, "stops example.txt", ",")
Msgbox(list1.Get(0),list1.Get(1))

I get in the message box :

[Ljav.lang.String;@....... as title

[Ljav.lang.String;@....... as message

I can't understand why , can someone help me ?

Thanks
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
LoadCSV returns a lis of string arrays, not a list of strings. You need to extract the strings from these arrays to display them.
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
Thanks for your reply, now I have understand , can you post the code to extract the array?
Maybe it should be like this:

Dim lista (100,3) as string

Lista = list1



Inviato dal mio H9500 con Tapatalk 2
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
Hi

where is the utility of list ?

For example i have a csv file like that:

Surname,name,birth
Smith,John,10/10/1970
Smith,John,10/10/1970
etc............................


normaly i use a 3 dimension array like lista(x,3) so the x index is the row and y index is the columm , and for example the lista(20,1) is the name field .
So i have one array to manage , to load the csv file to my array i was thinking to use the loadcsv function, but it passes through the list .
Is there a more fast way to load the csv to an array (and not the list)?

Excuse my English
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You can use something like this:
B4X:
dim ar2(list1.size,3)
for k=0 to list1.size-1
   dim ar()
   ar=regex.split(",",list1.get(k))
   for t=0 to 2
      ar2(k,t)=ar(t)
   next
next
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
Hi

I tried but it doesn't works.

It stops at line :

ar2(k,t)=ar(t)

k=0 and t=1 with error: index out of bound.

My Sub is :

B4X:
Dim su As StringUtils
Dim list1 As List

list1 = su.LoadCSV(File.DirAssets, "stops example.txt", ",")

Dim ar2(list1.size,3)

For k=0 To list1.size-1
   Dim ar()
   ar=Regex.split(",",list1.get(k))
    
   For t=0 To 2
      ar2(k,t)=ar(t)
   Next
Next

If i change the code to :

B4X:
Dim su As StringUtils
Dim list1 As List

list1 = su.LoadCSV(File.DirAssets, "stops example.txt", ",")

Dim ar2(list1.size,3)

For k=0 To list1.size-1
   Dim ar()
   ar=Regex.split(",",list1.get(k))
    
   Msgbox(ar(0),ar(0))
   
Next

The message box is : [Ljava.lang.String;@......

I can't understand !
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Add this sub to your project and watch the Log(). You will see all your imported data.

B4X:
Sub convCSV
   Dim su As StringUtils
   Dim list1 As List
   list1 = su.LoadCSV(File.DirAssets, "stops example.txt", ",")
   For i = 0 To list1.Size-1
      Dim sCol() As String 
      sCol = list1.Get(i)
      Dim NewRow As String
      For il = 0 To sCol.Length-1
         NewRow = NewRow & sCol(il) 
         If il < sCol.Length-1 Then
            NewRow = NewRow & " - "
         End If
      Next
      Log(NewRow)
   Next   
End Sub
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Hi

I tried but it doesn't works.

It stops at line :

ar2(k,t)=ar(t)

k=0 and t=1 with error: index out of bound.

My Sub is :

B4X:
Dim su As StringUtils
Dim list1 As List

list1 = su.LoadCSV(File.DirAssets, "stops example.txt", ",")

Dim ar2(list1.size,3)

For k=0 To list1.size-1
   Dim ar()
   ar=Regex.split(",",list1.get(k))
    
   For t=0 To 2
      ar2(k,t)=ar(t)
   Next
Next
If i change the code to :

B4X:
Dim su As StringUtils
Dim list1 As List

list1 = su.LoadCSV(File.DirAssets, "stops example.txt", ",")

Dim ar2(list1.size,3)

For k=0 To list1.size-1
   Dim ar()
   ar=Regex.split(",",list1.get(k))
    
   Msgbox(ar(0),ar(0))
   
Next
The message box is : [Ljava.lang.String;@......

I can't understand !

I can unfortunately. It was my mistake. I've just got into reading the structure of loadCSV, there's no need to regex.split, in reality it is wrong, since the return is already an array. Try this inside the loop:
B4X:
Dim ar()
    ar=list1.Get(k)
    For l=0 To ar.Length -1
        Msgbox(ar(l),"row " & (k+1) & " - column " & (l+1))
    Next
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Add this sub to your project and watch the Log(). You will see all your imported data.

B4X:
Sub convCSV
    Dim su As StringUtils
    Dim list1 As List
    list1 = su.LoadCSV(File.DirAssets, "stops example.txt", ",")
    For i = 0 To list1.Size-1
        Dim sCol() As String 
        sCol = list1.Get(i)
        Dim NewRow As String
        For il = 0 To sCol.Length-1
            NewRow = NewRow & sCol(il) 
            If il < sCol.Length-1 Then
                NewRow = NewRow & " - "
            End If
        Next
        Log(NewRow)
    Next    
End Sub

Yes that's it, I haven't checked your answer before posting :)
 
Upvote 0
Top