Page 1 of 1

structured map / list problem

Posted: Sun Feb 23, 2025 2:05 pm
by infratec
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

Re: structured map / list problem

Posted: Sun Feb 23, 2025 4:27 pm
by SMaag
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.

Re: structured map / list problem

Posted: Sun Feb 23, 2025 4:46 pm
by SMaag
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


Re: structured map / list problem

Posted: Sun Feb 23, 2025 5:11 pm
by infratec
Yes ... you are right. :oops:

And now I can remember that I already stumbled across this (not intuitiv) behavior.

Re: structured map / list problem

Posted: Sun Feb 23, 2025 8:00 pm
by idle
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.

Re: structured map / list problem

Posted: Sun Feb 23, 2025 9:26 pm
by skywalk
This is why I suffer structured arrays.
I only use Maps() and Lists() when speed is not necessary.