I'd want to know how many times a value is mentioned in a list. For instance list with following values:
blue
blue
green
yellow
needs to output
blue - 2
green - 1
yellow - 1
This is what Ive come up with, but it wont work :S
B4X:
Sub get_percentages(l As List) As List
Dim templist As List
Dim S As String
Dim output As List
l.Sort(True)
output.Initialize
templist.Initialize
templist = l
For i=0 To 3
S = l.Get(i)
For i=0 To templist.Size -1
If S = templist.Get(i) Then
Dim P As percentage
P.name = S
P.number = P.number +1
templist.Set(i, "counted")
End If
Next
If P.number > 0 Then output.Add(P)
Next
For i=0 To output.Size-1
Dim P As percentage
P= output.Get(i)
P.percentage = P.number/l.Size
output.Set(i, P)
Next
Return output
End Sub
Type percentage(name As String, percentage As Double, number As Int)
B4X:
Sub get_percentages(l As List) As List
Dim check As Boolean
Dim returnlist As List
Dim total As Int
total = l.Size
Log(total)
returnlist.Initialize
check = False
For i=0 To l.Size-1
Dim p As percentage
check = False
p.name = l.Get(i)
For j=0 To i-1
If l.Get(i) = l.Get(j) Then check = True
Next
If check = False Then
p.number = find_occurance(l,l.Get(i))
p.percentage = (p.number / total)
returnlist.Add(p)
End If
Next
Return returnlist
End Sub
Sub find_occurance(l As List, s As String) As Int
Dim counter As Int
For i=0 To l.Size-1
If l.Get(i)=s Then
counter = counter+1
End If
Next
Return counter
End Sub
Dim lst,lst2 As List
Dim k,k0,N As Int
lst.Initialize :lst2.Initialize
N=99
For k=0 To N
lst.Add ("item" & Rnd(1,11))
Next
lst.Sort (True)
k0=0
For k=0 To N-1
If lst.Get(k)<>lst.Get(k+1) Then
lst2.Add (lst.Get(k) & " - " & (k+1-k0))
k0=k+1
End If
Next
lst2.Add (lst.Get(N) & " - " & (N+1-k0))
Msgbox(lst,"input")
Msgbox(lst2,"result")
This exercise is ideal for a list generated from a SQLite table. Here is the full code:
B4X:
MyList.Initialize
txt="SELECT Color , COUNT(Color) AS ColorCount FROM MyTable " _
& "GROUP BY Color ORDER BY Color"
Cursor1=SQL1.ExecQuery(txt)
Cursor1.Position=0
For i=0 To Cursor1.RowCount-1
Cursor1.position=i
MyList.Add(Cursor1.GetString("Color") & Chr(9) & Cursor1.GetString("ColorCount"))
Next
Msgbox(MyList,"Final result")
thanks, this might be usefull in future projects. I have some experience with SQL because of Access and our WMS called Exceed.
At this point i'm not using a database for android apps.
And MC73, I like your approuch of things :sign0188: