T tonytoy New Member Licensed User Longtime User Apr 21, 2012 #1 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 stops example.txt 15.2 KB · Views: 710
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:
Erel B4X founder Staff member Licensed User Longtime User Apr 22, 2012 #2 See StringUtils.LoadCSV. Basic4android Search: StringUtils.LoadCSV Upvote 0
coslad Well-Known Member Licensed User Longtime User Nov 22, 2012 #3 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
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
K kickaha Well-Known Member Licensed User Longtime User Nov 23, 2012 #4 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
LoadCSV returns a lis of string arrays, not a list of strings. You need to extract the strings from these arrays to display them.
coslad Well-Known Member Licensed User Longtime User Nov 23, 2012 #5 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
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
Erel B4X founder Staff member Licensed User Longtime User Nov 23, 2012 #6 You do not need to create a two dimension array. You should iterate over the list items. Each item is a single dimension array. Upvote 0
You do not need to create a two dimension array. You should iterate over the list items. Each item is a single dimension array.
coslad Well-Known Member Licensed User Longtime User Nov 23, 2012 #7 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
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
mc73 Well-Known Member Licensed User Longtime User Nov 23, 2012 #8 You can use something like this: Code: Copy 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
You can use something like this: Code: Copy 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
coslad Well-Known Member Licensed User Longtime User Nov 23, 2012 #10 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 : Code: Copy 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 : Code: Copy 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
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 : Code: Copy 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 : Code: Copy 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 !
margret Well-Known Member Licensed User Longtime User Nov 23, 2012 #11 Add this sub to your project and watch the Log(). You will see all your imported data. Code: Copy 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: Nov 23, 2012 Upvote 0
Add this sub to your project and watch the Log(). You will see all your imported data. Code: Copy 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
mc73 Well-Known Member Licensed User Longtime User Nov 23, 2012 #12 coslad said: 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 : Code: Copy 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 : Code: Copy 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 ! Click to expand... 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: Code: Copy 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
coslad said: 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 : Code: Copy 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 : Code: Copy 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 ! Click to expand... 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: Code: Copy Dim ar() ar=list1.Get(k) For l=0 To ar.Length -1 Msgbox(ar(l),"row " & (k+1) & " - column " & (l+1)) Next
mc73 Well-Known Member Licensed User Longtime User Nov 23, 2012 #13 margret said: Add this sub to your project and watch the Log(). You will see all your imported data. Code: Copy 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 Click to expand... Yes that's it, I haven't checked your answer before posting Upvote 0
margret said: Add this sub to your project and watch the Log(). You will see all your imported data. Code: Copy 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 Click to expand... Yes that's it, I haven't checked your answer before posting
coslad Well-Known Member Licensed User Longtime User Nov 24, 2012 #14 Perfect ! Now it works , thanks ! Upvote 0