playMaker

Author Topic: ArrayMaker - HashTable random reorder  (Read 290 times)

nFighter

  • Beta Group
  • Full Member
  • *
  • Posts: 138
    • View Profile
    • multiplayer adult fighting
ArrayMaker - HashTable random reorder
« on: September 17, 2018, 12:16:04 AM »
Hello, fellow playmakers! I wonder, what is the most convenient way to shuffle  HashTable content randomly? There is a fast & simple action to shuffle the ArrayList, but there is no such action for HashTable.

For now I'm thinking about quite complex way like: transfer all the Keys from HastTable to the ArrayList, shuffle them and put to another HashTable. Then search for the Values one by one and transfer from old HashTable to new one. After that I can clear the old HashTable, and transfer all the key/value pairs from new table to old one.  :o :o :o

Did I missed something, maybe there could be some other simpler way?
indie developer of multiplayer adult fighting
http://nakedfighter3d.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14119
  • Official Playmaker Support
    • View Profile
Re: ArrayMaker - HashTable random reorder
« Reply #1 on: September 17, 2018, 11:07:47 PM »
Hi,

 HashTables are not meant to be randomized, because you access them via their keys.

 so yes, what you need to do is save the keys into an array list, and shuffle that list, and then iterate through that list when you want to get the hashtable related values. That's a perfectly valid process.

 Bye,

 Jean

nFighter

  • Beta Group
  • Full Member
  • *
  • Posts: 138
    • View Profile
    • multiplayer adult fighting
Re: ArrayMaker - HashTable random reorder
« Reply #2 on: September 18, 2018, 01:14:00 AM »
Oh, so my approach related on iterating tables with GetHasTableNext is not  a common using way  :-[
indie developer of multiplayer adult fighting
http://nakedfighter3d.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14119
  • Official Playmaker Support
    • View Profile
Re: ArrayMaker - HashTable random reorder
« Reply #3 on: September 18, 2018, 06:43:23 AM »
Hi,

 well, there is a multitude of means for iterating an hashtable, but as far as order, this is not the right way to do it indeed.

 one can iterate an hashtable simply to process data and it doesn't matter the order, which is the likely case, since this the case too with pur c#

https://lemire.me/blog/2011/08/29/why-cant-hash-tables-preserve-the-order-of-keys/

Bye,

 Jean