Dynamic variable names: Difference between revisions
(You don't do this in C) |
(Clarified task, noted that this isn't great style in Tcl and shown what is preferred instead.) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
{{task}}Create a variable with a user defined name. |
|||
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. |
|||
__TOC__ |
__TOC__ |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 10: | Line 11: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lang lisp> |
<lang lisp>(defmacro set-string (string value) |
||
(defmacro set-string (string value) |
|||
`(setf |
`(setf |
||
,(read-from-string string) |
,(read-from-string string) |
||
,value)) |
,value)) |
||
(set-string "dynamicA" "hello") |
|||
(print dynamicA) |
(print dynamicA)</lang> |
||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth> |
<lang forth>s" VARIABLE " pad swap move |
||
." Variable name: " pad 9 + 80 accept |
|||
⚫ | |||
." Variable name: " pad 9 + 80 accept |
|||
⚫ | |||
</lang> |
|||
Of course, it is easier for the user to simply type VARIABLE ''name'' at the Forth console. |
Of course, it is easier for the user to simply type VARIABLE ''name'' at the Forth console. |
||
Line 63: | Line 60: | ||
gets stdin varname |
gets stdin varname |
||
set $varname 0</lang> |
set $varname 0</lang> |
||
Note that it is more normal to use the user's name to index into a Tcl associative array, as the syntax gets easier to work with in that case: |
|||
<lang tcl>puts -nonewline "Enter an element name: "; flush stdout |
|||
gets stdin elemname |
|||
set ary($elemname) [expr int(rand()*100)] |
|||
puts "I have set element $elemname to $ary($elemname)"</lang> |
|||
[[Category:AutoHotkey_Originated]] |
[[Category:AutoHotkey_Originated]] |
Revision as of 08:36, 3 June 2009
You are encouraged to solve this task according to the task description, using any language you may know.
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.
AutoHotkey
<lang AutoHotkey> InputBox, Dynamic, Variable Name %Dynamic% = hello ListVars MsgBox % %dynamic% ; says hello </lang>
Common Lisp
<lang lisp>(defmacro set-string (string value)
`(setf ,(read-from-string string) ,value))
(set-string "dynamicA" "hello") (print dynamicA)</lang>
Forth
<lang forth>s" VARIABLE " pad swap move ." Variable name: " pad 9 + 80 accept pad swap 9 + evaluate</lang> Of course, it is easier for the user to simply type VARIABLE name at the Forth console.
Perl
<lang perl>print "Enter a variable name: "; $varname = <STDIN>; # type in "foo" on standard input chomp($varname); $$varname = 42; # when you try to dereference a string, it will be
# treated as a "symbolic reference", where they # take the string as the name of the variable
print "$foo\n"; # prints "42"</lang>
PHP
<lang php><?php $varname = rtrim(fgets(STDIN)); # type in "foo" on standard input $$varname = 42; echo "$foo\n"; # prints "42" ?></lang>
Python
<lang python>>>> n = raw_input("Enter a variable name: ") Enter a variable name: X >>> exec n + " = 42" >>> X 42</lang>
<lang python>>>> n = input("Enter a variable name: ") Enter a variable name: X >>> exec(n + " = 42") >>> X 42</lang>
Tcl
<lang Tcl>puts "Enter a variable name:" gets stdin varname set $varname 0</lang> Note that it is more normal to use the user's name to index into a Tcl associative array, as the syntax gets easier to work with in that case: <lang tcl>puts -nonewline "Enter an element name: "; flush stdout gets stdin elemname set ary($elemname) [expr int(rand()*100)] puts "I have set element $elemname to $ary($elemname)"</lang>