Jump to content

Dynamic variable names: Difference between revisions

m
Add Nim
(Added Kotlin)
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}}==
118

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.