Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Dictionary Variables??
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Planet_Fall




PostPosted: 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.


Turing:

/*  Programmer: Planet_Fall
 File Name:  Mole Mass
 */


%Variables
var H, Li, Be, B, C, N, O, F, Na, Mg, Al, Si, P, S, Cl, K, Ca : real

H := 1.00794
Li := 6.941
Be := 9.012182
B := 10.811
C := 12.0107
N := 14.0067
O := 15.994
F := 18.99840
Na := 22.989770
Mg := 24.3050
Al := 26.981538
Si := 28.0855
P := 30.97376
S := 32.065
Cl := 35.453
K := 39.0983
Ca := 40.078

put  H, Li, Be, B, C, N, O, F, Na, Mg, Al, Si, P, S, Cl, Na, Mg


Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: 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:

Turing:

type elementData:
    record
        symbol : string
        mass : real
    end record

var element : array 1 .. numElements of elementData

element (1).symbol := "H"
element (1).mass := 1.00794

element (2).symbol := "Li"
%etc, etc


You could simplify it further by doing this:

Turing:

function set_element (symbol : string, mass : real) : elementData
    var newElement : elementData
    newElement.symbol := symbol
    newElement.mass := mass

    result newElement
end set_element

element (1) := set_element ("H", 1.0079)
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: 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.
evildaddy911




PostPosted: Thu Dec 26, 2013 9:26 am   Post subject: RE:Dictionary Variables??

i feel like i should point out your missing helium...
np_123




PostPosted: 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
scholarlytutor




PostPosted: 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.
    
    
    
    



Periodic Table.t
 Description:

Download
 Filename:  Periodic Table.t
 Filesize:  1.41 KB
 Downloaded:  10 Time(s)

Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: