Associative array / dictionary / hash / map
|
04-28-2018, 02:53 AM
Post: #1
|
|||
|
|||
Associative array / dictionary / hash / map
A very useful data structures for programming is the associative array often called a dictionary or hash. I was missing it on the Prime and although there are references to a Prime command called 'table' it doesn't seem to work for me and word has it that it is deprecated anyway. So I wrote a really simple dictionary implementation that simply uses lists. We can't use matrices because they only allow numbers. So it has to be done with lists, which can store anything.
Typical dictionaries have a special syntax separating the key from the value e.g. notice the colon : Code:
The trick in my implementation is that every second item in the list is interpreted as the value. No special syntax required. Then just indent the list in such a way that the pairing of keys and values is apparent. Code:
Of course this implementation is not efficient - real hash tables are fast and don't need to search through each entry. But that is a bigger project and involves handling hash collisions etc. This implementation is fast enough for simple purposes. The next steps might be to support assignment and common operations like length, del, values() to return a list of values, keys() to return a list of keys etc. Since there is no OO in Prime I suppose all these operations would become separate functions. I may get around to writing them but feel free to contribute individual functions to this thread! FYI here is the complete list of dictionary functions that e.g. Python dictionaries support, and most languages (Php, Javascript, Ruby...) have identical functionality. |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Associative array / dictionary / hash / map - tcab - 04-28-2018 02:53 AM
RE: Associative array / dictionary / hash / map - parisse - 04-28-2018, 06:55 AM
|
User(s) browsing this thread: 2 Guest(s)