Dynamic variable names: Difference between revisions

Content deleted Content added
PureFox (talk | contribs)
Added Kotlin
Jlp765 (talk | contribs)
m Add Nim
Line 537:
A="GIBBERISH"
GIBBERISH=3.14159</lang>
 
=={{header|Nim}}==
Nim is a compiled language, with powerful Templating and Macros, which are compile-time rather than run-time.
 
This solution emulates dynamic variables by mapping a string to a pointer to a variable (using a table).
<lang Nim>import tables
 
var
theVar: int = 5
varMap = initTable[string, pointer]()
 
proc ptrToInt(p: pointer): int =
result = cast[ptr int](p)[]
 
proc main() =
write(stdout, "Enter a var name: ")
let sVar = readLine(stdin)
varMap.add($svar, theVar.addr)
echo "Variable ", sVar, " is ", ptrToInt(varMap[$sVar])
 
when isMainModule:
main()</lang>
Sample input/output :
{{out}}
<pre>Enter a var name: varZ
Variable varZ is 5</pre>
 
=={{header|Octave}}==