structured map / list problem

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

structured map / list problem

Post 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
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: structured map / list problem

Post 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.
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: structured map / list problem

Post 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

infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: structured map / list problem

Post by infratec »

Yes ... you are right. :oops:

And now I can remember that I already stumbled across this (not intuitiv) behavior.
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: structured map / list problem

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: structured map / list problem

Post by skywalk »

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
Post Reply