Lists and duplicate keys

Find quick help here to get you started with Hollywood
Post Reply
GMKai
Posts: 158
Joined: Mon Feb 15, 2010 10:58 am

Lists and duplicate keys

Post by GMKai »

When creating a list with numeric entries...like this:

Code: Select all

 Local indextab = CreateList()
 Local p  = ToNumber(mui.get(mymuiid,"active"))
 If(HaveItem(indextab,p) = False)
              InsertItem(indextab,p,p)
              DebugPrint("Inserted!")
           Else()
           ...
What would be a good way to check for duplicates before insertion?

That check above should work for my case, but fails when the loop comes around with the same value again?
Is there a function to check contents and not the index?
GMKai
Posts: 158
Joined: Mon Feb 15, 2010 10:58 am

Re: Lists and duplicate keys

Post by GMKai »

Could look like this:

Code: Select all

Local indextab = {}

For Local i = 0 To 10
    indextab[i]= i
Next

For Local i = 0 To 15
    DebugPrint("i: "..i)
    DebugPrint("Have: "..HaveItem(indextab,i))
Next

For Local i = 0 To 15
    If(HaveItem(indextab,i))
       DebugPrint("index filled")
    Else()
       DebugPrint("not filled")
    EndIf
    
Next


WaitLeftMouse()
Output:

Code: Select all

i: 0
Have: 1
i: 1
Have: 1
i: 2
Have: 1
i: 3
Have: 1
i: 4
Have: 1
i: 5
Have: 1
i: 6
Have: 1
i: 7
Have: 1
i: 8
Have: 1
i: 9
Have: 1
i: 10
Have: 1
i: 11
Have: 0
i: 12
Have: 0
i: 13
Have: 0
i: 14
Have: 0
i: 15
Have: 0
index filled
index filled
index filled
index filled
index filled
index filled
index filled
index filled
index filled
index filled
index filled
not filled
not filled
not filled
not filled
not filled
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Lists and duplicate keys

Post by airsoftsoftwair »

GMKai wrote: Sun Sep 24, 2023 5:16 pm That check above should work for my case, but fails when the loop comes around with the same value again?
Don't really understand what you're trying to do here. What do you mean by "fails when the loop comes around with the same value again"? I don't see that case in your code snippet. Please elaborate.
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Lists and duplicate keys

Post by Flinx »

I think the first code snippet does not work because it uses integer indices, so InsertItem() on an empty list will not insert at position p but on index 0.
Post Reply