Computer Science Canada Dictionary Variables?? |
Author: | Planet_Fall [ Wed Dec 25, 2013 11:40 pm ] | ||
Post subject: | Dictionary Variables?? | ||
Is there a dictionary variable for Turing like Ruby or Python? I'm trying to make a Periodic Table for Chemistry Class.
Please specify what version of Turing you are using Turing 4.1.1 |
Author: | Raknarg [ Thu Dec 26, 2013 12:15 am ] | ||||
Post subject: | RE:Dictionary Variables?? | ||||
No such thing as dictionaries here. However because you have an ordered set of information, it would make more sense anyways just to use arrays, like so:
You could simplify it further by doing this:
|
Author: | Tony [ Thu Dec 26, 2013 12:24 am ] |
Post subject: | RE:Dictionary Variables?? |
One of the advantages of a dictionary / hashmap, is that one can get the value 1.00794 by referencing element("H"), instead of element(1). As Raknarg points out, hashmaps are not build into Turing. |
Author: | Raknarg [ Thu Dec 26, 2013 1:42 am ] |
Post subject: | RE:Dictionary Variables?? |
I understand, but unless you want to build a hash table for Turing, that's what you get. Unless you wanted to build a data structure that worked like a very slow dictionary. |
Author: | evildaddy911 [ Thu Dec 26, 2013 9:26 am ] |
Post subject: | RE:Dictionary Variables?? |
i feel like i should point out your missing helium... |
Author: | np_123 [ Sun Dec 29, 2013 2:22 pm ] |
Post subject: | RE:Dictionary Variables?? |
Neon (Ne) and Argon (Ar) are missing from that list too... Unless you're purposely not including the noble gases |
Author: | scholarlytutor [ Thu Sep 24, 2020 11:31 pm ] |
Post subject: | Re: Dictionary Variables?? |
I stumbled upon this old thread and I wanted to point out two workarounds: If you were just writing a simple program, which I once did, to output a chemical name when the user types a key like H, a case block works: var element: string case element of label "H": put "Hydrogen" label "He": put "Helium" end case And so on. See my attachment. A more clever workaround uses enumerations: type Element : enum(H, He, Li) var PeriodicTable: array Element of string := init("Hydrogen", "Helium", "Lithium") put PeriodicTable (Element.H) It is more inconvenient because you have to write the 'Element.' each time before a key, but when you really need something that imitates a dictionary in Turing, this is the best solution I could think of. |