Map keys behave differently between PB and SB

Share your advanced PureBasic knowledge/code with the community.
User avatar
ljgww
User
User
Posts: 14
Joined: Fri Nov 02, 2007 6:37 pm
Location: Canada

Map keys behave differently between PB and SB

Post by ljgww »

It is not safe to assume that keys in the map are behaving in the same way when porting code forth and back between SpiderBasic and PureBasic.

Apparently SpiderBasic preserves map keys in order of populating while PureBasic does something else (likely some hashing for speed optimization)

the code:

Code: Select all

Procedure.s makeword()
  Protected.s word
  Protected.i i
  word = ""
  For i = 1 To 5:
    word + Chr(Random(90,65))
  Next 
  ProcedureReturn word
EndProcedure

Global NewList myList.s()
Global NewMap myMap.s()
Global i
Global.s word

Debug "Populate"
For i = 1 To 10:
  word = makeword()
  Debug word
  AddElement(myList())
  myList() = word
  AddMapElement(myMap(), word)
Next

Debug "List:"
ForEach myList()
  Debug myList()
Next

Debug "Map:"
ForEach myMap()
  Debug MapKey(myMap())
Next
SpiderBasic output:

Code: Select all

Populate
LTASV
GJMTS
JJIKJ
ATHOY
ZQSXC
JNFST
UFECP
KLJQQ
RYFRZ
ZHLYK

List:
LTASV
GJMTS
JJIKJ
ATHOY
ZQSXC
JNFST
UFECP
KLJQQ
RYFRZ
ZHLYK

Map:
LTASV
GJMTS
JJIKJ
ATHOY
ZQSXC
JNFST
UFECP
KLJQQ
RYFRZ
ZHLYK
PureBasic output:

Code: Select all

Populate
GLJPG
OBWPJ
QCDLB
TIMXS
YCWMJ
NPSKG
ZRNNL
AXGMG
YZWMQ
XMNYB

List:
GLJPG
OBWPJ
QCDLB
TIMXS
YCWMJ
NPSKG
ZRNNL
AXGMG
YZWMQ
XMNYB

Map:
ZRNNL
QCDLB
TIMXS
YZWMQ
AXGMG
GLJPG
XMNYB
OBWPJ
YCWMJ
NPSKG
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Map keys behave differently between PB and SB

Post by Fred »

True. It can also change if we implement a better algorythm in PB, you shouldn't rely on that.
Post Reply