Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Dynamic-variable-names/00-META.yaml
Normal file
3
Task/Dynamic-variable-names/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Dynamic_variable_names
|
||||
note: Programming environment operations
|
||||
10
Task/Dynamic-variable-names/00-TASK.txt
Normal file
10
Task/Dynamic-variable-names/00-TASK.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
;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.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
is←{ t←⍵ ⋄ ⎕this⍎⍺,'←t' } ⍝⍝ the 'Slick Willie' function ;)
|
||||
'test' is ⍳2 3
|
||||
test
|
||||
1 1 1 2 1 3
|
||||
2 1 2 2 2 3
|
||||
36
Task/Dynamic-variable-names/AWK/dynamic-variable-names.awk
Normal file
36
Task/Dynamic-variable-names/AWK/dynamic-variable-names.awk
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# syntax: GAWK -f DYNAMIC_VARIABLE_NAMES.AWK
|
||||
# Variables created in GAWK's internal SYMTAB (symbol table) can only be accessed via SYMTAB[name]
|
||||
BEGIN {
|
||||
PROCINFO["sorted_in"] = "@ind_str_asc"
|
||||
show_symbol_table()
|
||||
while (1) {
|
||||
printf("enter variable name? ")
|
||||
getline v_name
|
||||
if (v_name in SYMTAB) {
|
||||
printf("name already exists with a value of '%s'\n",SYMTAB[v_name])
|
||||
continue
|
||||
}
|
||||
if (v_name ~ /^$/) {
|
||||
printf("name is null\n")
|
||||
continue
|
||||
}
|
||||
if (v_name !~ /^[A-Za-z][A-Za-z0-9_]*$/) {
|
||||
printf("name illegally constructed\n")
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
printf("enter value? ")
|
||||
getline v_value
|
||||
SYMTAB[v_name] = v_value
|
||||
printf("variable '%s' has been created and assigned the value '%s'\n\n",v_name,v_value)
|
||||
show_symbol_table()
|
||||
exit(0)
|
||||
}
|
||||
function show_symbol_table( count,i) {
|
||||
for (i in SYMTAB) {
|
||||
printf("%s ",i)
|
||||
if (isarray(SYMTAB[i])) { count++ }
|
||||
}
|
||||
printf("\nsymbol table contains %d names of which %d are arrays\n\n",length(SYMTAB),count)
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
name: strip input "enter a variable name: "
|
||||
value: strip input "enter a variable value: "
|
||||
|
||||
let name value
|
||||
|
||||
print ["the value of variable" name "is:" var name]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
InputBox, Dynamic, Variable Name
|
||||
%Dynamic% = hello
|
||||
ListVars
|
||||
MsgBox % %dynamic% ; says hello
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
10 INPUT "Enter a variable name", v$
|
||||
20 KEYIN "LET "+v$+"=42"
|
||||
|
|
@ -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
|
||||
|
|
@ -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%!
|
||||
|
|
@ -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))
|
||||
);
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
using System.Dynamic;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
string varname = Console.ReadLine();
|
||||
//Let's pretend the user has entered "foo"
|
||||
dynamic expando = new ExpandoObject();
|
||||
var map = expando as IDictionary<string, object>;
|
||||
map.Add(varname, "Hello world!");
|
||||
|
||||
Console.WriteLine(expando.foo);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(eval `(def ~(symbol (read)) 42))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(setq var-name (read)) ; reads a name into var-name
|
||||
(set var-name 1) ; assigns the value 1 to a variable named as entered by the user
|
||||
|
|
@ -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))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
CL-USER> (rc-create-variable "GREETING" "hello")
|
||||
GREETING
|
||||
|
||||
CL-USER> (print greeting)
|
||||
"hello"
|
||||
27
Task/Dynamic-variable-names/E/dynamic-variable-names.e
Normal file
27
Task/Dynamic-variable-names/E/dynamic-variable-names.e
Normal 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!"]
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import system'dynamic;
|
||||
import extensions;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
object theVariables;
|
||||
|
||||
constructor()
|
||||
{
|
||||
theVariables := new DynamicStruct()
|
||||
}
|
||||
|
||||
function()
|
||||
{
|
||||
auto prop := new MessageName(console.write:"Enter the variable name:".readLine());
|
||||
(prop.setPropertyMessage())(theVariables,42);
|
||||
|
||||
console.printLine(prop.toPrintable(),"=",(prop.getPropertyMessage())(theVariables)).readChar()
|
||||
}
|
||||
}
|
||||
|
||||
public program = new TestClass();
|
||||
|
|
@ -0,0 +1 @@
|
|||
(set (intern (read-string "Enter variable name: ")) 123)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
--Add user-defined variable to the stack
|
||||
const VarName: io.prompt("Input Variable Name: "),
|
||||
VarValue: io.prompt("Input Variable Value: ")
|
||||
|
||||
debug.newvar(VarName,VarValue)
|
||||
|
||||
--Outputting the results
|
||||
log(debug.getvar(VarName))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
-module( dynamic_variable_names ).
|
||||
|
||||
-export( [task/0] ).
|
||||
|
||||
task() ->
|
||||
{ok,[Variable_name]} = io:fread( "Variable name? ", "~a" ),
|
||||
Form = runtime_evaluation:form_from_string( erlang:atom_to_list(Variable_name) ++ "." ),
|
||||
io:fwrite( "~p has value ~p~n", [Variable_name, runtime_evaluation:evaluate_form(Form, {Variable_name, 42})] ).
|
||||
|
|
@ -0,0 +1 @@
|
|||
42 readln set
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
s" VARIABLE " pad swap move
|
||||
." Variable name: " pad 9 + 80 accept
|
||||
pad swap 9 + evaluate
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type DynamicVariable
|
||||
As String name
|
||||
As String value
|
||||
End Type
|
||||
|
||||
Function FindVariableIndex(a() as DynamicVariable, v as String, nElements As Integer) As Integer
|
||||
v = LCase(Trim(v))
|
||||
For i As Integer = 1 To nElements
|
||||
If a(i).name = v Then Return i
|
||||
Next
|
||||
Return 0
|
||||
End Function
|
||||
|
||||
Dim As Integer n, index
|
||||
Dim As String v
|
||||
Cls
|
||||
|
||||
Do
|
||||
Input "How many variables do you want to create (max 5) "; n
|
||||
Loop Until n > 0 AndAlso n < 6
|
||||
|
||||
Dim a(1 To n) As DynamicVariable
|
||||
Print
|
||||
Print "OK, enter the variable names and their values, below"
|
||||
|
||||
For i As Integer = 1 to n
|
||||
Print
|
||||
Print " Variable"; i
|
||||
Input " Name : ", a(i).name
|
||||
a(i).name = LCase(Trim(a(i).name)) ' variable names are not case sensitive in FB
|
||||
If i > 0 Then
|
||||
index = FindVariableIndex(a(), a(i).name, i - 1)
|
||||
If index > 0 Then
|
||||
Print " Sorry, you've already created a variable of that name, try again"
|
||||
i -= 1
|
||||
Continue For
|
||||
End If
|
||||
End If
|
||||
Input " Value : ", a(i).value
|
||||
a(i).value = LCase(Trim(a(i).value))
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "Press q to quit"
|
||||
Do
|
||||
Print
|
||||
Input "Which variable do you want to inspect "; v
|
||||
If v = "q" OrElse v = "Q" Then Exit Do
|
||||
index = FindVariableIndex(a(), v, n)
|
||||
If index = 0 Then
|
||||
Print "Sorry there's no variable of that name, try again"
|
||||
Else
|
||||
Print "It's value is "; a(index).value
|
||||
End If
|
||||
Loop
|
||||
End
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
# As is, will not work if val is a String
|
||||
Assign := function(var, val)
|
||||
Read(InputTextString(Concatenation(var, " := ", String(val), ";")));
|
||||
end;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
defvar (intern 'This is not a pipe.') 42
|
||||
define |<weird>| 2009
|
||||
71
Task/Dynamic-variable-names/Go/dynamic-variable-names.go
Normal file
71
Task/Dynamic-variable-names/Go/dynamic-variable-names.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
n := 0
|
||||
for n < 1 || n > 5 {
|
||||
fmt.Print("How many integer variables do you want to create (max 5) : ")
|
||||
scanner.Scan()
|
||||
n, _ = strconv.Atoi(scanner.Text())
|
||||
check(scanner.Err())
|
||||
}
|
||||
vars := make(map[string]int)
|
||||
fmt.Println("OK, enter the variable names and their values, below")
|
||||
for i := 1; i <= n; {
|
||||
fmt.Println("\n Variable", i)
|
||||
fmt.Print(" Name : ")
|
||||
scanner.Scan()
|
||||
name := scanner.Text()
|
||||
check(scanner.Err())
|
||||
if _, ok := vars[name]; ok {
|
||||
fmt.Println(" Sorry, you've already created a variable of that name, try again")
|
||||
continue
|
||||
}
|
||||
var value int
|
||||
var err error
|
||||
for {
|
||||
fmt.Print(" Value : ")
|
||||
scanner.Scan()
|
||||
value, err = strconv.Atoi(scanner.Text())
|
||||
check(scanner.Err())
|
||||
if err != nil {
|
||||
fmt.Println(" Not a valid integer, try again")
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
vars[name] = value
|
||||
i++
|
||||
}
|
||||
fmt.Println("\nEnter q to quit")
|
||||
for {
|
||||
fmt.Print("\nWhich variable do you want to inspect : ")
|
||||
scanner.Scan()
|
||||
name := scanner.Text()
|
||||
check(scanner.Err())
|
||||
if s := strings.ToLower(name); s == "q" {
|
||||
return
|
||||
}
|
||||
v, ok := vars[name]
|
||||
if !ok {
|
||||
fmt.Println("Sorry there's no variable of that name, try again")
|
||||
} else {
|
||||
fmt.Println("It's value is", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def varname = 'foo'
|
||||
def value = 42
|
||||
|
||||
new GroovyShell(this.binding).evaluate("${varname} = ${value}")
|
||||
|
||||
assert foo == 42
|
||||
|
|
@ -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
|
||||
2
Task/Dynamic-variable-names/J/dynamic-variable-names-1.j
Normal file
2
Task/Dynamic-variable-names/J/dynamic-variable-names-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require 'misc'
|
||||
(prompt 'Enter variable name: ')=: 0
|
||||
5
Task/Dynamic-variable-names/J/dynamic-variable-names-2.j
Normal file
5
Task/Dynamic-variable-names/J/dynamic-variable-names-2.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
require 'misc'
|
||||
(prompt 'Enter variable name: ')=: 0
|
||||
Enter variable name: FOO
|
||||
FOO
|
||||
0
|
||||
4
Task/Dynamic-variable-names/J/dynamic-variable-names-3.j
Normal file
4
Task/Dynamic-variable-names/J/dynamic-variable-names-3.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
userDefined=: 'BAR'
|
||||
(userDefined)=: 1
|
||||
BAR
|
||||
1
|
||||
14
Task/Dynamic-variable-names/Java/dynamic-variable-names.java
Normal file
14
Task/Dynamic-variable-names/Java/dynamic-variable-names.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
public static void main(String... args){
|
||||
HashMap<String, Integer> vars = new HashMap<String, Integer>();
|
||||
//The variable name is stored as the String. The var type of the variable can be
|
||||
//changed by changing the second data type mentiones. However, it must be an object
|
||||
//or a wrapper class.
|
||||
vars.put("Variable name", 3); //declaration of variables
|
||||
vars.put("Next variable name", 5);
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String str = sc.next();
|
||||
vars.put(str, sc.nextInt()); //accpeting name and value from user
|
||||
|
||||
System.out.println(vars.get("Variable name")); //printing of values
|
||||
System.out.println(vars.get(str));
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var varname = 'foo'; // pretend a user input that
|
||||
var value = 42;
|
||||
eval('var ' + varname + '=' + value);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var varname = prompt('Variable name:');
|
||||
var value = 42;
|
||||
this[varname] = value;
|
||||
4
Task/Dynamic-variable-names/Jq/dynamic-variable-names.jq
Normal file
4
Task/Dynamic-variable-names/Jq/dynamic-variable-names.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"Enter a variable name:",
|
||||
(input as $var
|
||||
| ("Enter a value:" ,
|
||||
(input as $value | { ($var) : $value })))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
print("Insert the variable name: ")
|
||||
|
||||
variable = Symbol(readline(STDIN))
|
||||
expression = quote
|
||||
$variable = 42
|
||||
println("Inside quote:")
|
||||
@show $variable
|
||||
end
|
||||
|
||||
eval(expression)
|
||||
|
||||
println("Outside quote:")
|
||||
@show variable
|
||||
println("If I named the variable x:")
|
||||
@show x
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
// version 1.1.4
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var n: Int
|
||||
do {
|
||||
print("How many integer variables do you want to create (max 5) : ")
|
||||
n = readLine()!!.toInt()
|
||||
}
|
||||
while (n < 1 || n > 5)
|
||||
|
||||
val map = mutableMapOf<String, Int>()
|
||||
var name: String
|
||||
var value: Int
|
||||
var i = 1
|
||||
println("OK, enter the variable names and their values, below")
|
||||
do {
|
||||
println("\n Variable $i")
|
||||
print(" Name : ")
|
||||
name = readLine()!!
|
||||
if (map.containsKey(name)) {
|
||||
println(" Sorry, you've already created a variable of that name, try again")
|
||||
continue
|
||||
}
|
||||
print(" Value : ")
|
||||
value = readLine()!!.toInt()
|
||||
map.put(name, value)
|
||||
i++
|
||||
}
|
||||
while (i <= n)
|
||||
|
||||
println("\nEnter q to quit")
|
||||
var v: Int?
|
||||
while (true) {
|
||||
print("\nWhich variable do you want to inspect : ")
|
||||
name = readLine()!!
|
||||
if (name.toLowerCase() == "q") return
|
||||
v = map[name]
|
||||
if (v == null) println("Sorry there's no variable of that name, try again")
|
||||
else println("It's value is $v")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fn.print(Enter a variable name:\s)
|
||||
$varName = fn.input()
|
||||
$value = 42
|
||||
|
||||
fn.exec(\$$varName = \$value)
|
||||
|
||||
fn.println(fn.exec({{{return $}}}$varName))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
local(thename = web_request->param('thename')->asString)
|
||||
if(#thename->size) => {^
|
||||
var(#thename = math_random)
|
||||
var(#thename)
|
||||
else
|
||||
'<a href="?thename=xyz">Please give the variable a name!</a>'
|
||||
^}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
-- varName might contain a string that was entered by a user at runtime
|
||||
|
||||
-- A new global variable with a user-defined name can be created at runtime like this:
|
||||
(the globals)[varName] = 23 -- or (the globals).setProp(varName, 23)
|
||||
|
||||
-- An new instance variable (object property) with a user-defined name can be created at runtime like this:
|
||||
obj[varName] = 23 -- or obj.setProp(varName, 23)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
? make readword readword
|
||||
julie
|
||||
12
|
||||
? show :julie
|
||||
12
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
| ?- create_object(Id, [], [set_logtalk_flag(dynamic_declarations,allow)], []),
|
||||
write('Variable name: '), read(Name),
|
||||
write('Variable value: '), read(Value),
|
||||
Fact =.. [Name, Value],
|
||||
Id::assertz(Fact).
|
||||
|
||||
Variable name: foo.
|
||||
Variable value: 42.
|
||||
Id = o1,
|
||||
Name = foo,
|
||||
Value = 42,
|
||||
Fact = foo(42).
|
||||
|
||||
?- o1::current_predicate(foo/1).
|
||||
true.
|
||||
|
||||
| ?- o1::foo(X).
|
||||
X = 42.
|
||||
|
|
@ -0,0 +1 @@
|
|||
_G[io.read()] = 5 --puts 5 in a global variable named by the user
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
Module DynamicVariable {
|
||||
input "Variable Name:", a$
|
||||
a$=filter$(a$," ,+-*/^~'\({=<>})|!$&"+chr$(9)+chr$(127))
|
||||
While a$ ~ "..*" {a$=mid$(a$, 2)}
|
||||
If len(a$)=0 then Error "No name found"
|
||||
If chrcode(a$)<65 then Error "Not a Valid name"
|
||||
Inline a$+"=1000"
|
||||
Print eval(a$)=1000
|
||||
\\ use of a$ as pointer to variable
|
||||
a$.+=100
|
||||
Print eval(a$)=1100
|
||||
\\ list of variables
|
||||
List
|
||||
}
|
||||
Keyboard "George"+chr$(13)
|
||||
DynamicVariable
|
||||
5
Task/Dynamic-variable-names/M4/dynamic-variable-names.m4
Normal file
5
Task/Dynamic-variable-names/M4/dynamic-variable-names.m4
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Enter foo, please.
|
||||
define(`inp',esyscmd(`echoinp'))
|
||||
define(`trim',substr(inp,0,decr(len(inp))))
|
||||
define(trim,42)
|
||||
foo
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
USER>KILL ;Clean up workspace
|
||||
|
||||
USER>WRITE ;show all variables and definitions
|
||||
|
||||
USER>READ "Enter a variable name: ",A
|
||||
Enter a variable name: GIBBERISH
|
||||
USER>SET @A=3.14159
|
||||
|
||||
USER>WRITE
|
||||
|
||||
A="GIBBERISH"
|
||||
GIBBERISH=3.14159
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
varname = InputString["Enter a variable name"];
|
||||
varvalue = InputString["Enter a value"];
|
||||
ReleaseHold[ Hold[Set["nameholder", "value"]] /. {"nameholder" -> Symbol[varname], "value" -> varvalue}];
|
||||
Print[varname, " is now set to ", Symbol[varname]]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
/* Use :: for indirect assignment */
|
||||
block([name: read("name?"), x: read("value?")], name :: x);
|
||||
|
|
@ -0,0 +1 @@
|
|||
42 "Enter a variable name" ask define
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
print "Enter a variable name: "
|
||||
name = input()
|
||||
|
||||
print name + " = "
|
||||
exec(name + " = 42")
|
||||
exec("println " + name)
|
||||
17
Task/Dynamic-variable-names/Nim/dynamic-variable-names.nim
Normal file
17
Task/Dynamic-variable-names/Nim/dynamic-variable-names.nim
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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()
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
varname = input ("Enter variable name: ", "s");
|
||||
value = input ("Enter value: ", "s");
|
||||
eval([varname,"=",value]);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
: createVar(varname)
|
||||
"tvar: " varname + eval ;
|
||||
|
||||
"myvar" createVar
|
||||
|
||||
12 myvar put
|
||||
myvar at .
|
||||
|
|
@ -0,0 +1 @@
|
|||
eval(Str(input(), "=34"))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
$varname = rtrim(fgets(STDIN)); # type in "foo" on standard input
|
||||
$$varname = 42;
|
||||
echo "$foo\n"; # prints "42"
|
||||
?>
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
PROGRAM ExDynVar;
|
||||
|
||||
{$IFDEF FPC}
|
||||
{$mode objfpc}{$H+}{$J-}{R+}
|
||||
{$ELSE}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
|
||||
(*)
|
||||
Free Pascal Compiler version 3.2.0 [2020/06/14] for x86_64
|
||||
The free and readable alternative at C/C++ speeds
|
||||
compiles natively to almost any platform, including raspberry PI
|
||||
|
||||
This demo uses a dictionary because it is compiled: it cannot make
|
||||
dynamic variables at runtime.
|
||||
(*)
|
||||
|
||||
USES
|
||||
Generics.Collections,
|
||||
SysUtils,
|
||||
Variants;
|
||||
|
||||
TYPE
|
||||
|
||||
Tdict =
|
||||
{$IFDEF FPC}
|
||||
specialize
|
||||
{$ENDIF}
|
||||
TDictionary < ansistring, variant > ;
|
||||
|
||||
VAR
|
||||
VarName: ansistring;
|
||||
strValue: ansistring;
|
||||
VarValue: variant;
|
||||
D: Tdict;
|
||||
|
||||
FUNCTION SetType ( strVal: ansistring ) : variant ;
|
||||
|
||||
(*)
|
||||
If the value is numeric, store it as numeric, otherwise store it as ansistring
|
||||
(*)
|
||||
|
||||
BEGIN
|
||||
|
||||
TRY
|
||||
SetType := StrToFloat ( strVal ) ;
|
||||
EXCEPT
|
||||
SetType := strVal ;
|
||||
END;
|
||||
|
||||
END;
|
||||
|
||||
BEGIN
|
||||
|
||||
D := TDict.Create;
|
||||
REPEAT
|
||||
Write ( 'Enter variable name : ' ) ;
|
||||
ReadLn ( VarName ) ;
|
||||
Write ( 'Enter variable Value : ' ) ;
|
||||
ReadLn ( strValue ) ;
|
||||
VarValue := SetType ( strValue ) ;
|
||||
TRY
|
||||
BEGIN
|
||||
D.AddOrSetValue ( VarName, VarValue ) ;
|
||||
Write ( VarName ) ;
|
||||
Write ( ' = ' ) ;
|
||||
WriteLn ( D [ VarName ] ) ;
|
||||
END;
|
||||
EXCEPT
|
||||
WriteLn ( 'Something went wrong.. Try again' ) ;
|
||||
END;
|
||||
UNTIL ( strValue = '' ) ;
|
||||
D.Free;
|
||||
|
||||
END.
|
||||
|
|
@ -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"
|
||||
|
|
@ -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"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
constant globals = new_dict()
|
||||
|
||||
while 1 do
|
||||
string name = prompt_string("Enter name or press Enter to quit:")
|
||||
if length(name)=0 then exit end if
|
||||
bool bExists = (getd_index(name,globals)!=NULL)
|
||||
string prompt = iff(not bExists?"No such name, enter a value:"
|
||||
:sprintf("Already exists, new value[%s]:",{getd(name,globals)}))
|
||||
string data = prompt_string(prompt)
|
||||
if length(data) then
|
||||
setd(name,data,globals)
|
||||
end if
|
||||
end while
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
requires("0.8.2")
|
||||
class dc dynamic
|
||||
-- public string fred = "555" -- (predefine some fields if you like)
|
||||
end class
|
||||
dc d = new()
|
||||
|
||||
while 1 do
|
||||
string name = prompt_string("Enter name or press Enter to quit:")
|
||||
if length(name)=0 then exit end if
|
||||
bool bExists = (get_field_type(d,name)!=NULL)
|
||||
-- bool bExists = string(d[name]) -- alt...
|
||||
string prompt = iff(not bExists?"No such name, enter a value:"
|
||||
:sprintf("Already exists, new value[%s]:",{d[name]}))
|
||||
string data = prompt_string(prompt)
|
||||
if length(data) then
|
||||
d[name] = data
|
||||
end if
|
||||
end while
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
$variableName = Read-Host
|
||||
New-Variable $variableName 'Foo'
|
||||
Get-Variable $variableName
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
editvar /newvar /value=a /userinput=1 /title=Enter a variable name:
|
||||
editvar /newvar /value=b /userinput=1 /title=Enter a variable title:
|
||||
editvar /newvar /value=-a- /title=-b-
|
||||
|
|
@ -0,0 +1 @@
|
|||
test :- read(Name), atomics_to_string([Name, "= 50, writeln('", Name, "' = " , Name, ")"], String), term_string(Term, String), Term.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
?- test.
|
||||
|: "Foo".
|
||||
Foo = 50.
|
||||
true.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>>> name = raw_input("Enter a variable name: ")
|
||||
Enter a variable name: X
|
||||
>>> globals()[name] = 42
|
||||
>>> X
|
||||
42
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>>> name = input("Enter a variable name: ")
|
||||
Enter a variable name: X
|
||||
>>> globals()[name] = 42
|
||||
>>> X
|
||||
42
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
[ say "The word "
|
||||
dup echo$
|
||||
names find names found iff
|
||||
[ say " exists." ]
|
||||
else
|
||||
[ say " does not exist." ] ] is exists? ( $ --> )
|
||||
|
||||
|
||||
[ $ "Please enter a name: " input
|
||||
cr
|
||||
dup exists?
|
||||
cr cr
|
||||
dup say "Creating " echo$
|
||||
say "..."
|
||||
$ "[ stack ] is " over join quackery
|
||||
cr cr
|
||||
exists? cr ] is task ( --> )
|
||||
10
Task/Dynamic-variable-names/R/dynamic-variable-names.r
Normal file
10
Task/Dynamic-variable-names/R/dynamic-variable-names.r
Normal 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)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
REBOL [
|
||||
Title: "Dynamic Variable Name"
|
||||
URL: http://rosettacode.org/wiki/Dynamic_variable_names
|
||||
]
|
||||
|
||||
; Here, I ask the user for a name, then convert it to a word and
|
||||
; assign the value "Hello!" to it. To read this phrase, realize that
|
||||
; REBOL collects terms from right to left, so "Hello!" is stored for
|
||||
; future use, then the prompt string "Variable name? " is used as the
|
||||
; argument to ask (prompts user for input). The result of ask is
|
||||
; converted to a word so it can be an identifier, then the 'set' word
|
||||
; accepts the new word and the string ("Hello!") to be assigned.
|
||||
|
||||
set to-word ask "Variable name? " "Hello!"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
/*REXX program demonstrates the use of dynamic variable names & setting a val.*/
|
||||
parse arg newVar newValue
|
||||
say 'Arguments as they were entered via the command line: ' newVar newValue
|
||||
say
|
||||
call value newVar, newValue
|
||||
say 'The newly assigned value (as per the VALUE bif)------' newVar value(newVar)
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>> s = "myusername"
|
||||
myusername
|
||||
>> $$.[s] = 10;
|
||||
>> myusername
|
||||
10
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
-> (begin (printf "Enter some name: ")
|
||||
(namespace-set-variable-value! (read) 123))
|
||||
Enter some name: bleh
|
||||
-> bleh
|
||||
123
|
||||
11
Task/Dynamic-variable-names/Raku/dynamic-variable-names.raku
Normal file
11
Task/Dynamic-variable-names/Raku/dynamic-variable-names.raku
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
our $our-var = 'The our var';
|
||||
my $my-var = 'The my var';
|
||||
|
||||
my $name = prompt 'Variable name: ';
|
||||
my $value = $::('name'); # use the right sigil, etc
|
||||
|
||||
put qq/Var ($name) starts with value 「$value」/;
|
||||
|
||||
$::('name') = 137;
|
||||
|
||||
put qq/Var ($name) ends with value 「{$::('name')}」/;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
:newVariable ("-) s:get var ;
|
||||
|
||||
newVariable: foo
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
See "Enter the variable name: " give cName eval(cName+"=10")
|
||||
See "The variable name = " + cName + " and the variable value = " + eval("return "+cName) + nl
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Enter the variable name: test
|
||||
The variable name = test and the variable value = 10
|
||||
|
|
@ -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}"
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
* # Get var name from user
|
||||
output = 'Enter variable name:'
|
||||
invar = trim(input)
|
||||
|
||||
* # Get value from user, assign
|
||||
output = 'Enter value:'
|
||||
$invar = trim(input)
|
||||
|
||||
* Display
|
||||
output = invar ' == ' $invar
|
||||
end
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
=> (define (create-variable name initial-val)
|
||||
(eval `(define ,name ,initial-val) (interaction-environment)))
|
||||
|
||||
=> (create-variable (read) 50)
|
||||
<hello
|
||||
|
||||
=> hello
|
||||
50
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
var name = read("Enter a variable name: ", String); # type in 'foo'
|
||||
|
||||
class DynamicVar(name, value) {
|
||||
method init {
|
||||
DynamicVar.def_method(name, ->(_) { value })
|
||||
}
|
||||
}
|
||||
|
||||
var v = DynamicVar(name, 42); # creates a dynamic variable
|
||||
say v.foo; # retrieves the value
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
define: #name -> (query: 'Enter a variable name: ') intern. "X"
|
||||
define: name -> 42.
|
||||
X print.
|
||||
|
|
@ -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).
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
| varName |
|
||||
varName := Stdin request: 'Enter a global variable name:'.
|
||||
Smalltalk at:varName asSymbol put:42.
|
||||
expr := Stdin request:'Enter an expression:'.
|
||||
(Compiler evaluate:expr) printCR
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
display "Name?" _request(s)
|
||||
scalar $s=10
|
||||
display $s
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Local varName,value
|
||||
InputStr "Variable name", varName
|
||||
Prompt value
|
||||
value → #varName
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
$$ MODE TUSCRIPT
|
||||
ASK "Enter variablename": name=""
|
||||
ASK "Enter value": value=""
|
||||
TRACE +@name
|
||||
@name=$value
|
||||
PRINT @name
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
puts "Enter a variable name:"
|
||||
gets stdin varname
|
||||
set $varname 42
|
||||
puts "I have set variable $varname to [set $varname]"
|
||||
|
|
@ -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)"
|
||||
|
|
@ -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])"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
read name
|
||||
declare $name=42
|
||||
echo "${name}=${!name}"
|
||||
18
Task/Dynamic-variable-names/Wren/dynamic-variable-names.wren
Normal file
18
Task/Dynamic-variable-names/Wren/dynamic-variable-names.wren
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import "io" for Stdin, Stdout
|
||||
|
||||
var userVars = {}
|
||||
System.print("Enter three variables:")
|
||||
for (i in 0..2) {
|
||||
System.write("\n name : ")
|
||||
Stdout.flush()
|
||||
var name = Stdin.readLine()
|
||||
System.write(" value: ")
|
||||
Stdout.flush()
|
||||
var value = Num.fromString(Stdin.readLine())
|
||||
userVars[name] = value
|
||||
}
|
||||
|
||||
System.print("\nYour variables are:\n")
|
||||
for (kv in userVars) {
|
||||
System.print(" %(kv.key) = %(kv.value)")
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
org &8000
|
||||
WaitChar equ &BB06 ;Amstrad CPC BIOS call, loops until user presses a key. That key's ASCII value is returned in A.
|
||||
PrintChar equ &BB5A ;Amstrad CPC BIOS call, A is treated as an ASCII value and is printed to the screen.
|
||||
|
||||
getInput:
|
||||
call WaitChar
|
||||
;returns key press in A
|
||||
|
||||
|
||||
or a ;set flags according to accumulator
|
||||
jp m,getInput
|
||||
;most keyboards aren't capable of going over ascii 127
|
||||
;but just in case they can prevent it.
|
||||
;IX/IY offsets are signed, thus a key press outside of 7-bit ASCII would index out of bounds
|
||||
|
||||
push af
|
||||
call PrintChar ;prints the user variable name to the screen.
|
||||
pop af
|
||||
|
||||
call NewLine
|
||||
|
||||
ld (LoadFromUserNamedVariable+2),a ;offset byte is at addr+2
|
||||
ld (StoreToUserNamedVariable+2),a
|
||||
|
||||
; This self-modifying code turns both instances of (IX+0) into (IX+varname)
|
||||
|
||||
|
||||
ld a,&42 ;set the value of the dynamically named variable
|
||||
; to &42
|
||||
|
||||
ld ix,ExtraRam ;storage location of dynamically named variables
|
||||
|
||||
StoreToUserNamedVariable:
|
||||
ld (IX+0),a ;store 42 at the named offset
|
||||
;"+0" is overwritten with the dynamic user ram name
|
||||
|
||||
xor a
|
||||
dec a
|
||||
;just to prove that the value is indeed stored where the code
|
||||
; is intending to, set A to 255 so that the next section of
|
||||
; code will show that the variable is indeed retrieved and
|
||||
; is shown to the screen
|
||||
|
||||
LoadFromUserNamedVariable:
|
||||
ld a,(IX+0) ;retrieve the value at the stored offset. The "+0" was overwritten with the user-defined offset.
|
||||
|
||||
call ShowHex ;prints to the terminal the value stored at the dynamically named user variable
|
||||
|
||||
|
||||
ReturnToBasic
|
||||
RET
|
||||
|
||||
ShowHex: ;credit to Keith S. of Chibiakumas
|
||||
push af
|
||||
and %11110000
|
||||
rrca
|
||||
rrca
|
||||
rrca
|
||||
rrca
|
||||
call PrintHexChar
|
||||
pop af
|
||||
and %00001111
|
||||
;call PrintHexChar
|
||||
;execution flows into it naturally.
|
||||
PrintHexChar:
|
||||
or a ;Clear Carry Flag
|
||||
daa
|
||||
add a,&F0
|
||||
adc a,&40
|
||||
jp PrintChar
|
||||
;ret
|
||||
|
||||
NewLine:
|
||||
push af
|
||||
ld a,13 ;Carriage return
|
||||
call PrintChar
|
||||
ld a,10 ;Line Feed
|
||||
call PrintChar
|
||||
pop af
|
||||
ret
|
||||
|
||||
|
||||
org &9000
|
||||
ExtraRam:
|
||||
ds 256,0 ;256 bytes of ram, each initialized to zero
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
vname:="foo"; // or vname:=ask("var name = ");
|
||||
klass:=Compiler.Compiler.compileText("var %s=123".fmt(vname))(); // compile & run the constructor
|
||||
klass.vars.println();
|
||||
klass.foo.println();
|
||||
klass.setVar(vname).println(); // setVar(name,val) sets the var
|
||||
Loading…
Add table
Add a link
Reference in a new issue