B4J Question How to get index of an element in a typed list

Bruce Axtens

Active Member
Licensed User
Longtime User
I have a List containing Typed entries
B4X:
Sub Process_Globals
    Private restServer As Server
    Type Proxy(ip As String, efficacy As Int)
    Public proxyList As List
End Sub

Sub AppStart (Args() As String)
    proxyList.Initialize()
    Dim pList As List
    pList = File.ReadList("c:\web\proxies.txt","")
    Dim i As Int
    For i = 0 To pList.Size - 1
        Dim p As Proxy
        p.Initialize()
        p.ip = pList.Get(i)
        p.efficacy = 0
        proxyList.Add(p)
    Next
Later on in the code I want to find a matching element in the List by its ip, but I don't know what it's efficacy is. Thus, the following doesn't work
B4X:
        Case "DELETE" ' Delete
            response.Write(request.Method & " " & request.FullRequestURI & "<br>")
            ip = request.GetParameter("ip")
            Dim pos As Int
            pos = Main.proxyList.IndexOf(ip)
            response.Write(pos & " for " & ip)
How do I locate the matching ip in the List?

BTW,
B4X:
proxyRec.ip = ip
pos = Main.proxyList.IndexOf(proxyRec)
doesn't work either. Should I be using a Map?
 
Last edited:

eurojam

Well-Known Member
Licensed User
Longtime User
your proxylist contains your own type proxy. Therefore you have to loop trough your list to find it or you can hold the ip in a second list - your plist (make it global and public), then your position will be
B4X:
pos = Main.pList.IndexOf(ip)
the position in plist will be the same as in proxylist
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
So two lists, one for the proxy itself and one for the efficacy. Hmmm ... yeah, should work ok. Thanks.
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
Oh hang on a sec. There's one thing that might throw that idea a bit:
At the bottom of the Handler is
B4X:
    Main.proxyList.SortType("efficacy",False)

So I'm sorting on efficacy. The proxies that perform the best rise to the top.

So having two lists works fine until you want to sort on efficacy and have the proxies reorder with them.
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
For now I'm doing
B4X:
            For pos = 0 To Main.proxyList.Size
                proxyRec = Main.proxyList.Get(pos)
                If proxyRec.ip = ip Then
                    proxyRec.efficacy = proxyRec.efficacy + 1
                    Main.proxyList.Set(pos,proxyRec)
                    Exit 
                End If
            Next
            response.Write(proxyRec.efficacy)
but hoping one day for a faster way
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
Wasn't aware of that syntax. Looks good. Thanks.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top