Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Dictionary example

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Dictionary example

    Hi Catventure,

    use is quite general.
    Take this example:
    [code=thinbasic]

    USES "dictionary"

    Dim pInventoryDict As Long ' -- Pointer to dictionary

    %NUM_Characters = 3
    pInventoryDict = Dictionary_Create(%NUM_Characters * 2) ' -- Character name + inventory

    Dictionary_Add(pInventoryDict, "Old orc", "Axe, Sword, Deodorant")
    Dictionary_Add(pInventoryDict, "Wizard", "Magic wand, Beard dummy")
    Dictionary_Add(pInventoryDict, "Woodpecker", "Red hat")

    Msgbox 0, "Wizard invenotry is:"+$CRLF+$CRLF+Dictionary_Find(pInventoryDict, "Wizard")

    Dictionary_Free(pInventoryDict)
    [/code]

    ... in this case you can use it like array, but instead of numeric index use directly name of character


    Bye,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2

    Re: Dictionary example

    Hi Petr,

    Sorry still don't get it!

    What exactly is dictionary?

    Look to be good idea I could use in adventure.

    I've been out out tonite and had a few drinks so not thinking properly!

    catventure
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Dictionary example

    Hi,

    this was just one example, maybe not the best.
    I will try to take a glass of Jameson and see if it can be understood differentely then ;D

    Imagine you need to store some data, which can be hardly indexed using numbers.
    You can still do that using arrays + equates, to keep it clear, but this is not as universal.

    So you can index items using strings, this is how I understand it
    Eros released new thinBASIC preview with nice example on dictionaries, worth to have a look !


    Bye,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4

    Re: Dictionary example

    Thanks Psch,

    I have recovered a bit from last night

    I understand your example better now. Very good.
    Well this definitely opens up some new possibilities, and I wasn't expecting anything like this to be added to thinBasic, so fantastic job!

    catventure
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Dictionary example

    he he ;D
    I lived in London for one year working for a multinational company.
    Because I do not drink any alchool (beer included) I remember my "emargination" during first months. Most couldn't bealive I didn't drink anything other than water :P
    Than people started to appreciate my no alchool attitude. I was always the "driver" to get them home.

    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Dictionary example

    I am happy you can see it now

    ;D Eros, this is nice story!


    Bye,
    Petr


    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Dictionary example

    Petr, I don't understand this code:
    %NUM_Characters = 3
    pInventoryDict = Dictionary_Create(%NUM_Characters * 2) ' -- Character name + inventory

    Why not:
    pInventoryDict = Dictionary_Create(3 * 2)
    or
    pInventoryDict = Dictionary_Create(6)
    or
    pInventoryDict = Dictionary_Create(3 * sizeof(char)) : I don't understand where the 3 comes from when the words are much longer characters than 3 or 3*2 of 6 characters?
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Dictionary example

    Ken,

    it is always good programming practice to define constants (rather than writing fixed numbers) even if it will be longer or boring.
    This mainly for 2 important factors: readability and maintainability.

    Readability:
    it give much more sense to read something like %MaxNumberOfEnemies rather than something like 10 or 6 or 100

    Maintainability:
    if along the code you need to use again that constant it will be much, much more easy to maintain just a constant value rathar then searching all the code for all the places where you use a number and change it.

    Also consider len of variable will not make any difference in compiled languages and a minimum difference in thinBasic too because every token is just parsed once.
    So, use all the power of the language to always create code to be readable and maintanable.

    Ciao
    Eros


    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Dictionary example

    Thanks Eros, can you explain where the 3 and 2 came from to size the dictionary with the create command, how do you determine those values?
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Dictionary example

    Hi kryton,

    it is simple, we have 3 characters...
    Each of them has assigned some data ( inventory ).

    So if we need 3 entries for names to dictionary, we will use numberOfCharacters + numberOfInventories. As number of inventories is the same as numberOfCharacters, I put there * 2. It is always about pair of data. So keys * 2.

    This is also explained under thinBasic language / Modules / Dictionary / Dictionary_Create in help file.

    You can imagine it as "How many cells I need to stor following?":


    "Old orc""Axe, Sword, Deodorant"


    "Wizard""Magic wand, Beard dummy"


    "Woodpecker""Red hat"



    Bye,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Page 1 of 2 12 LastLast

Similar Threads

  1. The Skeptics Dictionary
    By Charles Pegge in forum Shout Box Area
    Replies: 0
    Last Post: 12-07-2011, 00:14
  2. Dictionary speed example
    By ErosOlmi in forum Dictionary module
    Replies: 10
    Last Post: 22-04-2007, 22:48

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •