This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,4 @@
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.

View file

@ -0,0 +1,2 @@
---
note: Programming environment operations

View file

@ -0,0 +1,4 @@
InputBox, Dynamic, Variable Name
%Dynamic% = hello
ListVars
MsgBox % %dynamic% ; says hello

View file

@ -0,0 +1,2 @@
10 INPUT "Enter a variable name", v$
20 KEYIN "LET "+v$+"=42"

View file

@ -0,0 +1,7 @@
INPUT "Enter a variable name: " name$
INPUT "Enter a numeric value: " numeric$
dummy% = EVAL("FNassign("+name$+","+numeric$+")")
PRINT "Variable " name$ " now has the value "; EVAL(name$)
END
DEF FNassign(RETURN n, v) : n = v : = 0

View file

@ -0,0 +1,14 @@
@echo off
setlocal enableDelayedExpansion
set /p "name=Enter a variable name: "
set /p "value=Enter a value: "
::Create the variable and set its value
set "%name%=%value%"
::Display the value without delayed expansion
call echo %name%=%%%name%%%
::Display the value using delayed expansion
echo %name%=!%name%!

View file

@ -0,0 +1,9 @@
( put$"Enter a variable name: "
& get$:?name
& whl
' ( put$"Enter a numeric value: "
& get$:?numeric:~#
)
& !numeric:?!name
& put$(str$("Variable " !name " now has the value " !!name \n))
);

View file

@ -0,0 +1 @@
(eval `(def ~(symbol (read)) 42))

View file

@ -0,0 +1,6 @@
(defun rc-create-variable (name initial-value)
"Create a global variable whose name is NAME in the current package and which is bound to INITIAL-VALUE."
(let ((symbol (intern name)))
(proclaim `(special ,symbol))
(setf (symbol-value symbol) initial-value)
symbol))

View file

@ -0,0 +1,5 @@
CL-USER> (rc-create-variable "GREETING" "hello")
GREETING
CL-USER> (print greeting)
"hello"

View file

@ -0,0 +1,27 @@
def makeNounExpr := <elang:evm.makeNounExpr>
def dynVarName(name) {
def variable := makeNounExpr(null, name, null)
return e`{
def a := 1
def b := 2
def c := 3
{
def $variable := "BOO!"
[a, b, c]
}
}`.eval(safeScope)
}
? dynVarName("foo")
# value: [1, 2, 3]
? dynVarName("b")
# value: [1, "BOO!", 3]
? dynVarName("c")
# value: [1, 2, "BOO!"]

View file

@ -0,0 +1,3 @@
s" VARIABLE " pad swap move
." Variable name: " pad 9 + 80 accept
pad swap 9 + evaluate

View file

@ -0,0 +1,6 @@
data Var a = Var String a deriving Show
main = do
putStrLn "please enter you variable name"
vName <- getLine
let var = Var vName 42
putStrLn $ "this is your variable: " ++ show var

View file

@ -0,0 +1,3 @@
var varname = 'foo'; // pretend a user input that
var value = 42;
eval('var ' + varname + '=' + value);

View file

@ -0,0 +1 @@
_G[io.read()] = 5 --puts 5 in a global variable named by the user

View file

@ -0,0 +1,5 @@
<?php
$varname = rtrim(fgets(STDIN)); # type in "foo" on standard input
$$varname = 42;
echo "$foo\n"; # prints "42"
?>

View file

@ -0,0 +1,7 @@
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"

View file

@ -0,0 +1,9 @@
use strict;
print "Enter a variable name: ";
my $foo;
my $varname = <STDIN>; # type in "foo" on standard input
chomp($varname);
my $varref = eval('\$' . $varname);
$$varref = 42;
print "$foo\n"; # prints "42"

View file

@ -0,0 +1,6 @@
(de userVariable ()
(prin "Enter a variable name: ")
(let Var (line T) # Read transient symbol
(prin "Enter a value: ")
(set Var (read)) # Set symbol's value
(println 'Variable Var 'Value (val Var)) ) ) # Print them

View file

@ -0,0 +1,5 @@
>>> name = raw_input("Enter a variable name: ")
Enter a variable name: X
>>> globals()[name] = 42
>>> X
42

View file

@ -0,0 +1,5 @@
>>> name = input("Enter a variable name: ")
Enter a variable name: X
>>> globals()[name] = 42
>>> X
42

View file

@ -0,0 +1,10 @@
# Read the name in from a command prompt
varname <- readline("Please name your variable >")
# Make sure the name is valid for a variable
varname <- make.names(varname)
message(paste("The variable being assigned is '", varname, "'"))
# Assign the variable (with value 42) into the user workspace (global environment)
assign(varname, 42)
#Check that the value has been assigned ok
ls(pattern=varname)
get(varname)

View file

@ -0,0 +1,7 @@
/*REXX program to show use of dynamic variable names. */
parse arg new value
say 'Arguments as they were entered via the command line =' new value
say
call value new,value
say 'The newly assigned value (as per the VALUE bif)------' new value(new)

View file

@ -0,0 +1,4 @@
p "Enter a variable name"
x = gets.chomp!
instance_variable_set "@" + x, 42
p "The value of #{x} is #{instance_variable_get "@" + x}"

View file

@ -0,0 +1,8 @@
=> (define (create-variable name initial-val)
(eval `(define ,name ,initial-val) (interaction-environment)))
=> (create-variable (read) 50)
<hello
=> hello
50

View file

@ -0,0 +1,9 @@
| varName |
varName := FillInTheBlankMorph
request: 'Enter a variable name'.
Compiler
evaluate: '| ', varName, ' | ', varName, ' := 42.
Transcript
show: ''value of ', varName, ''';
show: '' is '';
show: ', varName.

View file

@ -0,0 +1,4 @@
puts "Enter a variable name:"
gets stdin varname
set $varname 42
puts "I have set variable $varname to [set $varname]"

View file

@ -0,0 +1,4 @@
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)"

View file

@ -0,0 +1,5 @@
puts -nonewline "Enter a variable name: "; flush stdout
gets stdin varname
upvar 0 $varname v; # The 0 for current scope
set v [expr int(rand()*100)]
puts "I have set variable $varname to $v (see for yourself: [set $varname])"