Computer Science Canada Convert a string into a variable name. |
| Author: | Timothy Willard [ Wed Jun 26, 2013 5:07 pm ] | ||
| Post subject: | Convert a string into a variable name. | ||
What is it you are trying to achieve? I want to be able to take a string (for example: var teststring := "maxHealth") and be able to convert it into a variable name so it could be used (ex. if I were to type teststring := 50 with the syntax it would set the variable maxHealth to 50, not try to set the string teststring to 50). What is the problem you are having? As far as I am aware, there is no syntax for this in Turing, and I will have to hardcode each string in with an if statement (i.e.
Describe what you have tried to solve this problem I have looked around on google, and found answers in other programming languages (I think square brackets do what I want in javascript, but I didn't look to closely). I can't find it in Turing, and I assume it doesn't exist, but I am checking to make sure. Please specify what version of Turing you are using 4.1 |
|||
| Author: | DemonWasp [ Wed Jun 26, 2013 5:26 pm ] |
| Post subject: | RE:Convert a string into a variable name. |
What you're talking about it usually called reflection: http://en.wikipedia.org/wiki/Reflection_(computer_programming) . A lesser version is introspection, which is a much weaker related idea. Turing has no support for reflection or introspection. |
|
| Author: | Zren [ Wed Jun 26, 2013 8:08 pm ] |
| Post subject: | RE:Convert a string into a variable name. |
Quote: found answers in other programming languages (I think square brackets do what I want in javascript, but I didn't look to closely).
Those are called dictionaries. They tend to use the same syntax for array indexes as well. They are also known as Mappings in Java (Map and HashMap). They are key-value store objects. Turing doesn't have a native implementation for them. There are a Turing libraries that implements dictionaries, or rather HashMap, though it's very verbose. http://compsci.ca/v3/viewtopic.php?t=20302 OpenTuring has a HashMap implementation, but that won't help you if you if you're submitting for a school project. |
|