Page 1 of 1
Lists and duplicate keys
Posted: Sun Sep 24, 2023 5:16 pm
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?
Re: Lists and duplicate keys
Posted: Tue Sep 26, 2023 7:02 pm
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
Re: Lists and duplicate keys
Posted: Tue Oct 03, 2023 12:29 pm
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.
Re: Lists and duplicate keys
Posted: Wed Oct 04, 2023 11:39 am
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.