Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Posts: 7662 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Sun Feb 23, 2025 2:05 pm
Maybe I'm blind today.
This code:
Code: Select all
Structure PG_DPList_Structure
value$
EndStructure
Structure PG_DL_Structure
List PG_DPList.PG_DPList_Structure()
EndStructure
Structure PG_Structure
List DList.PG_DL_Structure()
EndStructure
NewMap PCGMap.PG_Structure()
AddMapElement(PCGMap(), "0")
If FindMapElement(PCGMap(), "0")
Debug "MapElement 0 found"
If Not SelectElement(PCGMap()\DList(), 0)
Debug "Data Add 0"
AddElement(PCGMap()\DList())
EndIf
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "01"
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "02"
EndIf
Debug ""
If FindMapElement(PCGMap(), "0")
Debug "MapElement 0 found"
If Not SelectElement(PCGMap()\DList(), 1)
Debug "Data Add 1"
AddElement(PCGMap()\DList())
EndIf
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "11"
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "12"
EndIf
Debug ""
If FindMapElement(PCGMap(), "0")
Debug "MapElement 0 found"
If SelectElement(PCGMap()\DList(), 0)
Debug "Selected list element 0"
ForEach PCGMap()\DList()\PG_DPList()
Debug "Value " + PCGMap()\DList()\PG_DPList()\value$
Next
EndIf
Debug ""
If SelectElement(PCGMap()\DList(), 1)
Debug "Selected list element 1"
ForEach PCGMap()\DList()\PG_DPList()
Debug "Value " + PCGMap()\DList()\PG_DPList()\value$
Next
EndIf
EndIf
Returns the wrong list elements:
MapElement 0 found
Data Add 0
MapElement 0 found
Data Add 1
MapElement 0 found
Selected list element 0
Value 11
Value 12
Selected list element 1
Value 01
Value 02
I expect:
Selected 0
Value 01
Value 02
Selected 1
Value 11
Value 12
Or do I make a msitake?
PB 6.20 x86 asm backend on Windows 10 X64
SMaag
Enthusiast
Posts: 327 Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany
Post
by SMaag » Sun Feb 23, 2025 4:27 pm
I can confirm, it do not work like expected!
First Element (No 0) is : 11, 12 not 1,2
What do you want to do with this kind of hirarchical Structures?
If there is any error it will be very hard to debug.
SMaag
Enthusiast
Posts: 327 Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany
Post
by SMaag » Sun Feb 23, 2025 4:46 pm
I tested once again!
Yes the Listentry 0 is 11,12 but this is correct.
The reason is the
AddElement after "If Not SelectElement(PCGMap()\DList(), 1)"
See the PB Help the new Element is added at first position of the List.
Here the modified code
Code: Select all
Structure PG_DPList_Structure
value$
EndStructure
Structure PG_DL_Structure
List PG_DPList.PG_DPList_Structure()
EndStructure
Structure PG_Structure
List DList.PG_DL_Structure()
EndStructure
NewMap PCGMap.PG_Structure()
AddMapElement(PCGMap(), "0")
If FindMapElement(PCGMap(), "0")
Debug "MapElement 0 found"
If Not SelectElement(PCGMap()\DList(), 0)
Debug "Data Add 0"
AddElement(PCGMap()\DList())
EndIf
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "01"
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "02"
EndIf
Debug ""
If FindMapElement(PCGMap(), "0")
Debug "MapElement 0 found"
If Not SelectElement(PCGMap()\DList(), 1)
Debug "Data Add 1"
LastElement(PCGMap()\DList()) ; <======================== Jump to last!
AddElement(PCGMap()\DList())
EndIf
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "11"
AddElement(PCGMap()\DList()\PG_DPList())
PCGMap()\DList()\PG_DPList()\value$ = "12"
EndIf
Debug ""
If FindMapElement(PCGMap(), "0")
Debug "MapElement 0 found"
If SelectElement(PCGMap()\DList(), 0)
Debug "Selected list element 0"
ForEach PCGMap()\DList()\PG_DPList()
Debug "Value " + PCGMap()\DList()\PG_DPList()\value$
Next
EndIf
Debug ""
If SelectElement(PCGMap()\DList(), 1)
Debug "Selected list element 1"
ForEach PCGMap()\DList()\PG_DPList()
Debug "Value " + PCGMap()\DList()\PG_DPList()\value$
Next
EndIf
EndIf
infratec
Always Here
Posts: 7662 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Sun Feb 23, 2025 5:11 pm
Yes ... you are right.
And now I can remember that I already stumbled across this (not intuitiv) behavior.
idle
Always Here
Posts: 6026 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sun Feb 23, 2025 8:00 pm
Off topic pb lists are a bit odd for instance you make a LIFO of ABCD
Firstelement AddElement will result in order
ADCB
Resetlist AddElement will result in
DCBA
It's caught me out a couple of times.
skywalk
Addict
Posts: 4241 Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA
Post
by skywalk » Sun Feb 23, 2025 9:26 pm
This is why I suffer structured arrays.
I only use Maps() and Lists() when speed is not necessary.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum