RosettaCodeData/Task/Dynamic-variable-names/EMal/dynamic-variable-names.emal

38 lines
1.2 KiB
Plaintext

^|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))