Share your advanced knowledge/code with the community.
ljgww
 
Posts: 26
Joined: Thu Mar 26, 2020 5:47 pm

Map keys behave differently between PB and SB

by ljgww Mon May 25, 2020 2:26 pm

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
Return to Tricks 'n' Tips

Who is online

Users browsing this forum: No registered users and 4 guests