RosettaCodeData/Task/Dynamic-variable-names/Phix/dynamic-variable-names-2.phix

19 lines
594 B
Plaintext

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