Task

Create a variable with a user-defined name.

The variable name should not be written in the program text, but should be taken from the user dynamically.



See also
   Eval in environment is a similar task.


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[$svar] = theVar.addr
  echo "Variable ", sVar, " is ", ptrToInt(varMap[$sVar])
 
when isMainModule:
  main()