Filter csv file

Windsailor

Member
Licensed User
Longtime User
Hello,

How do I filter a csv file that I am loading into a scrollview?

For instance I have a csv file that has dates / data for the whole year and I would like to load only the values for a single month instead of the whole year (or some other variable).

Which leads into the next question, is there a wildcard option that is supported?

Currently my 'Date' values are in the context of "01/01" (don't need the year value for right now). If there isn't a wildcard option I guess I could separate the date values into different columns.

And where would this be the most optimized?

Thanks
 

Windsailor

Member
Licensed User
Longtime User
I guess another way to do this is to load the complete csv file into the scrollview and then edit or filter it using the AutoCompleteEditText.

I would like to learn both ways if possible.

I have mirrored the example on creating and populating the scrollview by code, now I just have to figure how to generate 'all' of them by code to fit.

Thanks
 
Upvote 0

Windsailor

Member
Licensed User
Longtime User
I tried stepping through the code and found where the complete row was to be initially inserted and then tried to do a comparison (text or value contains x)... but I can't find the right syntax (don't think it is supported at that level within b4a) to do the comparison.

The AutoCompleteEditText does this in a way but I can not find how to enable the same properties or events and apply them to a filter variable. So I am assuming that it is on a different level.

Thanks
 
Upvote 0

Windsailor

Member
Licensed User
Longtime User
:cool:
I have not seen that one. Much appreciated.
I'm going to be busy the next couple of days and then I can play with it and see what I can do.

Thanks
 
Upvote 0

Windsailor

Member
Licensed User
Longtime User
I found the tutorial of Regex expressions by Erel.

Where I am having a problem is:

Dim row() As String
row = List1.Get(i)

If I hover the mouse above 'row' I can see the values that are to be inserted into that particular row -encased in brackets.

If I declare a variable 'text_to_be_checked_against' as string and assign it the text value of 'row';

Dim text_to_be_checked_against as String
text_to_be_checked_against = row

I get the value or text of: [Ljave.lang.String;@43e721f0 (Not the original values that were shown when hovering the mouse over it).
Which now fails in the regex matcher.

I guess I should ask how do I get the string value or values of the items in List1?

Or am I heading in the wrong direction?:sign0104:

Thanks
 
Upvote 0

Windsailor

Member
Licensed User
Longtime User
Unfortunately that had the same out come; returning ([Ljava.lang.String; etc)

When I tried to declare:
Dim row As String

Instead of:
Dim row() As String

I received an error of "Cannot cast type {Type=String,Rank=0} to {Type=String,Rank=1} when I called:
AddRow1(row)

I tried different variations of the regex.split; commas, brackets and commas etc. and I still received the Ljava.lang.String;

Time for a break... and to re-think this.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It would be much easier if you posted your whole project.
Just with very small code snippets without knowing what you want to do and how it is very difficult to give efficient advices.
Try following code, it's the same as the one I posted in the previous post, just renamed the variables.
B4X:
Dim row() As String 
Dim rowitem As String 
rowitem = List1.Get(i) 
row = Regex.Split(",", rowitem)
You must replace the comma by your separation character.
I didn't know that you used the 'row' variable elsewhere.

Best regards.
 
Upvote 0

Windsailor

Member
Licensed User
Longtime User
Sorry I should have mentioned exactly I was working on. I initially thought I could get a few hints and work this through. But that didn't work.

I'm actually experimenting with the tutorial/sample "TableExample2". I simply copied that code over into my test project and was trying to do a couple of things with that sample. You know tweak things and see what happens.

The code in the sub is:

B4X:
'Clears the previous Table1 and loads the CSV file to the Table1
Sub LoadTableFromCSV1(Dir As String, Filename As String, HeadersExist As Boolean)
   ClearAll1
   Dim List1 As List
   Dim h() As String
   If HeadersExist Then
      Dim headers As List
      List1 = StringUtils1.LoadCSV2(Dir, Filename, ",", headers)
      Dim h(headers.Size) As String
      For i = 0 To headers.Size - 1
         h(i) = headers.Get(i)
      Next
   Else
      List1 = StringUtils1.LoadCSV(Dir, FileName, ",")
      Dim firstRow() As String
      firstRow = List1.Get(0)
      Dim h(firstRow.Length)
      For i = 0 To firstRow.Length - 1
         h(i) = "Col" & (i + 1)
      Next
   End If
   NumberOfColumns1 = h.Length
   ColumnWidth1 = SV1.Width / NumberOfColumns1 'update the columns widths
   ColumnWidth1_1 = ColumnWidth1-ColLineWidth
   SetHeader1(h)
   For i = 0 To List1.Size - 1
      Dim row() As String
      row = List1.Get(i)

                'Adding checks for filter on csv file being imported at this point

      AddRow1(row)
   Next
End Sub

Inside of the tutorial or sample, I was inserting the test 'filter' after line 84. Have to start somewhere.
Stepping through that sample, 'row' (line 84) shows one value but returns another. Not sure if that is the right place to do a comparison for a filter... hope that helps explaining things.

Thanks
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi,

i have written some functions for CSV reading (Ansi / UFT8) line by line.
You will find the sample and the code files here:
http://www.b4x.com/forum/german-forum/12036-windows-csv-einlesen.html#post70563
The sample is in german, but the file itself is in englisch.
What to do is just read the CSV File line by line
m = HBCSV.getmap() reads the first line, and all the others.
To filter you can just compare if some of m.get("fieldname") = "what you want"

BUT the fieldname by now is exactly like it is in the CSV. If you are not shure that the names there will be every time the same, you have to find out witch column is the right one.
 
Upvote 0
Top