Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
37
Task/Dynamic-variable-names/EMal/dynamic-variable-names.emal
Normal file
37
Task/Dynamic-variable-names/EMal/dynamic-variable-names.emal
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
^|EMal doesn't support the creation of user-defined variable names.
|
||||
|It can manage symbolic references to existing variables.
|
||||
|It can use Records to dynamically create fields.
|
||||
|^
|
||||
Record data ← []
|
||||
fun fillData ← void by block
|
||||
for ever
|
||||
^|we ask for key/value pairs|^
|
||||
text key ← ask(text, "Enter the key or press Enter to quit: ")
|
||||
if key æ Text.EMPTY do break end
|
||||
text value ← ask(text, "Enter the value: ")
|
||||
data[key] ← value ^|here we set the user-defined key field
|
||||
|on the record data, with the provided value
|
||||
|^
|
||||
end
|
||||
writeLine()
|
||||
end
|
||||
^|here we show how symbolic reference works by using var("variableName")|^
|
||||
fun dumpData ← void by text recordName
|
||||
writeLine("dumping the fields for the record named '", recordName, "'")
|
||||
Record.keys(var(recordName)).list(<text key|
|
||||
writeLine(" ", key, " ⇒ ", var(recordName)[key]))
|
||||
end
|
||||
^|
|
||||
|fillData()
|
||||
|dumpData(ask(text, "Enter the name of the variable (must be 'data'): "))
|
||||
|^
|
||||
text recordName
|
||||
if Runtime.args.length æ 3
|
||||
data[Runtime.args[0]] ← Runtime.args[1]
|
||||
recordName ← Runtime.args[2]
|
||||
else
|
||||
fillData()
|
||||
end
|
||||
dumpData(when(recordName æ Text.EMPTY,
|
||||
ask(text, "Enter the name of the variable (must be 'data'): "),
|
||||
recordName))
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
import os
|
||||
import strconv
|
||||
|
||||
fn main() {
|
||||
mut n, mut value, mut i := 0, 0, 1
|
||||
mut name :=""
|
||||
mut vars := map[string]int{}
|
||||
for n < 1 || n > 5 {
|
||||
n = strconv.atoi(os.input("Integer variables (max 5): ")) or {println("Invalid input!") continue}
|
||||
}
|
||||
for i <= n {
|
||||
println("OK, enter the variable names and their values, below:")
|
||||
println("Variable ${i}")
|
||||
name = os.input_opt(" Name: ") or {println("Invalid input!") exit(1)}
|
||||
for {
|
||||
value = strconv.atoi(os.input(" Value: ")) or {println("Must by a number!") continue}
|
||||
break
|
||||
}
|
||||
vars[name] += value
|
||||
i++
|
||||
}
|
||||
println("\n" + "Enter q to quit")
|
||||
for {
|
||||
name = os.input_opt("Which variable do you want to inspect: ") or {println("Invalid input!") exit(1)}
|
||||
if name.to_lower() == "q" {break}
|
||||
if name !in vars {println("Sorry there's no variable of that name, try again!")}
|
||||
else {println("Its value is ${vars[name]}")}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue