B4J Question CSV contain names of variables?!?!?

aidymp

Well-Known Member
Licensed User
Longtime User
Hi I have a need for a download counter for some of my zip files!

I have the download counters output via php and i get output like a CSV

So

Zipfromjack,102
bobsZip,3049
philszip,393
...

is my list.

I can read the list and currently use a long winded way to read them like so code originally from @margret's example

B4X:
DIM BOBZIP, ZIPFROMJACK, PHILSZIP As String

Sub convCSV
   Dim su As StringUtils
   Dim list1 As List
   list1 = su.LoadCSV(File.DirApp, "test.csv", ",")
   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
                 Select NewRow
                           Case "Zipfromjack"
                                  ZIPFROMJACK=sCol(il+1)
                                  Log ("ZipFromJack = "&ZIPFROMJACK)
                         
                           Case "bobsZip"
                                  BOBSZIP=sCol(il+1)
                                  Log ("bobsZip = "&BOBSZIP)
                         
                           Case "philszip"
                                  PHILSZIP=sCol(il+1)
                                  Log ("PhilsZip = "&PHILSZIP)
                 End Select
            'NewRow = NewRow & " - " - Removed for example
         End If
      Next
      'Log(NewRow) - Removed for example
   Next
End Sub

The question is rather than do all the Case "bobszip" how can I load the values into the correct variables?

Thanks

Aidy
 

stevel05

Expert
Licensed User
Longtime User
Try this:
B4X:
    Dim M As Map
    M.Initialize
    Dim L As List = Array As String("Zipfromjack,102","bobsZip,3049","philszip,393")
  
    For Each S As String In L
        Dim P As Int = S.IndexOf(",")
        M.Put(S.SubString2(0,P),S.SubString(P+1))
    Next
  
    Dim BOBZIP As Int = M.Get("bobsZip")
    Dim ZIPFROMJACK As Int = M.Get("Zipfromjack")
    Dim PHILSZIP As Int = M.Get("philszip")
  
    Log(BOBZIP)
    Log(ZIPFROMJACK)
    Log(PHILSZIP)

I've used the text case from your example.
 
Last edited:
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
    Dim M As Map
    M.Initialize
    Dim L As List = Array As String("Zipfromjack,102","bobsZip,3049","philszip,393")
  
    For Each S As String In L
        Dim P As Int = S.IndexOf(",")
        M.Put(S.SubString2(0,P),S.SubString(P+1))
    Next
  
    Dim BOBZIP As Int = M.Get("bobsZip")
    Dim ZIPFROMJACK As Int = M.Get("Zipfromjack")
    Dim PHILSZIP As Int = M.Get("philszip")
  
    Log(BOBZIP)
    Log(ZIPFROMJACK)
    Log(PHILSZIP)

I've used the text case from you example.

Hi, Thanks im just wondering how I would get the CSV file into the map? as I need to use live data

Thanks

Aidy
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The CSV file would go in the List, as per your original code, the Map is created from the list. I should have used your original variable names to make it clearer:

B4X:
Dim M As Map
    M.Initialize
    'Just load some test data
    Dim List1 As List = Array As String("Zipfromjack,102","bobsZip,3049","philszip,393")
    'Replaces your code for testing
    'Dim list1 AsList
     'list1 = su.LoadCSV(File.DirApp, "test.csv", ",")
 
    For Each NewRow As String In List1
        Dim P As Int = NewRow.IndexOf(",")
        M.Put(NewRow.SubString2(0,P),NewRow.SubString(P+1))
    Next
 
    Dim BOBZIP As Int = M.Get("bobsZip")
    Dim ZIPFROMJACK As Int = M.Get("Zipfromjack")
    Dim PHILSZIP As Int = M.Get("philszip")
 
    Log(BOBZIP)
    Log(ZIPFROMJACK)
    Log(PHILSZIP)
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
The CSV file would go in the List, as per your original code, the Map is created from the list. I should have used your original variable names to make it clearer:

B4X:
Dim M As Map
    M.Initialize
    'Just load some test data
    Dim List1 As List = Array As String("Zipfromjack,102","bobsZip,3049","philszip,393")
    'Replaces your code for testing
    'Dim list1 AsList
     'list1 = su.LoadCSV(File.DirApp, "test.csv", ",")

    For Each NewRow As String In List1
        Dim P As Int = NewRow.IndexOf(",")
        M.Put(NewRow.SubString2(0,P),NewRow.SubString(P+1))
    Next

    Dim BOBZIP As Int = M.Get("bobsZip")
    Dim ZIPFROMJACK As Int = M.Get("Zipfromjack")
    Dim PHILSZIP As Int = M.Get("philszip")

    Log(BOBZIP)
    Log(ZIPFROMJACK)
    Log(PHILSZIP)

Hi, Thanks again thats great, I have modified the code and now i dont even have to make all the variables, BOBZIP etc. I just call it counter, and where its displayed get the map for the counter I require! MAPS really are useful once you know how to use them! lol (I knew what a map was, but didn't know how to load data into it) I think I can now say hello to smaller code!

So Thanks again...

Aidy
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you are new to Map's then I suggest you also add some resilience into the code. If there will only be one item for each name in the data, you could uppercase (or lowercase) the names as you write it to the map. It saves the possibility of mistyping the case when you try to get the data. Also use GetDefault when trying to read so you don't get a null returned if the item is missing, which may cause problems:

B4X:
    Dim M As Map
    M.Initialize
    Dim List1 As List = Array As String("Zipfromjack,102","bobsZip,3049","philszip,393")
   
    For Each S As String In List1
        Dim ThisRow As Int = S.IndexOf(",")
        M.Put(S.SubString2(0,ThisRow).ToUpperCase,S.SubString(ThisRow+1))
    Next
   
    Dim BOBSZIP As Int = M.GetDefault("BOBSZIP",0)
    Dim ZIPFROMJACK As Int = M.GetDefault("ZIPFROMJACK",0)
    Dim PHILSZIP As Int = M.GetDEfault("PHILSZIP",0)
   
    Log(BOBSZIP)
    Log(ZIPFROMJACK)
    Log(PHILSZIP)
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
If you are new to Map's then I suggest you also add some resilience into the code. If there will only be one item for each name in the data, you could uppercase (or lowercase) the names as you write it to the map. It saves the possibility of mistyping the case when you try to get the data. Also use GetDefault when trying to read so you don't get a null returned if the item is missing, which may cause problems:

B4X:
    Dim M As Map
    M.Initialize
    Dim List1 As List = Array As String("Zipfromjack,102","bobsZip,3049","philszip,393")
  
    For Each S As String In List1
        Dim ThisRow As Int = S.IndexOf(",")
        M.Put(S.SubString2(0,ThisRow).ToUpperCase,S.SubString(ThisRow+1))
    Next
  
    Dim BOBSZIP As Int = M.GetDefault("BOBSZIP",0)
    Dim ZIPFROMJACK As Int = M.GetDefault("ZIPFROMJACK",0)
    Dim PHILSZIP As Int = M.GetDEfault("PHILSZIP",0)
  
    Log(BOBSZIP)
    Log(ZIPFROMJACK)
    Log(PHILSZIP)


Hi Thanks for the tip, I was just looking at my database and it is all oddly cased! so was looking at that!

You, like most people in this forum are very helpful and Make B4X more accessable than any language I have used!

Thanks!

Aidy
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
I would just like to say maps are AMAZING! lol I have managed to cut around 60 variables and around 400 lines of code out of Both the Windows and Android App.

Im a few steps closer to having my app working purely off a database, using PHP, and maps!

Thanks!

Aidy
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Can anyone help me clarify some information??

These maps are really useful as I said above! but im still hard coding some info in the app! as in the above example with the BOBSZIP, ZIPFROMJACK, PHILSZIP
records, I still make an initial list in the app to match the maps too!

So as I cant find a tutorial on the For Each command, am i right in presuming that reading the data into a map (initially) and then using the "For Each something as something) I could make the app completely work of the database, and add or remove things without having to update the code of the app!

A little update on the above so we you can see what i am doing!

I have a database with 5 fields, 1st field, contains a unique identifier, the other fields contain info like file name, file location, file image, file date
I have 4 maps! all use the 1st field as the key, and the data is from one of the remaining 4 fields! is this how maps are supposed to be used??

so I can just use urlmap.get("BOBSZIP") and it returns the url, and same locationmap.get("BOBSZIP") and I get the file location!

so im very close but as I said I want it to work purely off the database.

All the information is stored in a list (in one form or another, as I use coverflow, and customtlists for windows and android apps, but its in a list!)

So instead of doing the initial list creation manually. It looks like its I can make the list with a For Each ..... Is this correct??

If so can anyone point me to a good tutorial on For Each, as searching is not giving the info I need, I do see some examples but they are not really clear (to me at least)

Thanks

Aidy
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Quick example sort of relating to what you want
B4X:
 ...
	Dim m As Map
	m.Initialize
	Dim item(4) As Object
	item = Array("one","two","three","four") ' these could be your db fields
	m.Put("key1",item) ' put into map
	For Each i() As Object In m.Values   ' iterate over the values in the map 
		For Each j As Object In i        ' iterate over the array
			Log(j)						 ' print out the data 	
		Next	
	Next
 ....
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Quick example sort of relating to what you want
B4X:
...
    Dim m As Map
    m.Initialize
    Dim item(4) As Object
    item = Array("one","two","three","four") ' these could be your db fields
    m.Put("key1",item) ' put into map
    For Each i() As Object In m.Values   ' iterate over the values in the map
        For Each j As Object In i        ' iterate over the array
            Log(j)                         ' print out the data    
        Next   
    Next
....


Hi, thank you! but your going to regret answering lol!

B4X:
For Each j as Object in i

the "j" is that random? or is there a meaning to it?

Thank's

Aidy
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The variable after the
B4X:
for each
can be called anything you want, it just has to be the correct type.
 
Upvote 0
Top