Hi All,
This is probably a 'dumb A**' question.
Is there a way to match a variable name to a string ?
E.G string.s = "VARIABLE_1" variable(string) = "ABC"
I'd like to store variables and values in a text file but the only way I can think if reading them back is with if then or select case and every variable name - which will be a bit of a pain.
Thanks for any advice.
Cheers
Match variable name to string ?
- captain_skank
- Enthusiast
- Posts: 636
- Joined: Fri Oct 06, 2006 3:57 pm
- Location: England
Re: Match variable name to string ?
I'm not sure if this is a solution (or if I understood the question correctly), but Lib Preference allows you to list, read, write names and values even if they are not known in advance (see NextPreferenceKey())
Re: Match variable name to string ?
Alternative: use data in JSON format which allows to "dump" and "reload" all variables in one file at once.
(But now I can't help, I still don't understand anything about using this format)
(But now I can't help, I still don't understand anything about using this format)
- captain_skank
- Enthusiast
- Posts: 636
- Joined: Fri Oct 06, 2006 3:57 pm
- Location: England
Re: Match variable name to string ?
See you learn something every day - i did not even know about preferences
Anyhoo that that is not really what i after.
what I'm tring to do is populate a variable from a file which is fine using something like :
but i want to store the variable name and variable value ( on the same line ) in a file.
then loop through the file and variable name = variable value without hardcoding the varibale names in IF or SELECT statements.
But havinh thought about it I don't think it's possible.
Anyhoo that that is not really what i after.
what I'm tring to do is populate a variable from a file which is fine using something like :
Code: Select all
abc = [content from file]
then loop through the file and variable name = variable value without hardcoding the varibale names in IF or SELECT statements.
But havinh thought about it I don't think it's possible.
Re: Match variable name to string ?
I don't understand what's being asked. Can you explain it differently?
Re: Match variable name to string ?
Only with maps.Code: Select all
abc = [content from file]
where MapKey is [abc] and the value is [content from file],
and the disadvantage that all variables are of one type.
String is IMHO the best for such a thing.
like this ...
Code: Select all
Global NewMap exVar.s()
....
String.s = ReadString(FileID)
exVar(Trim(StringField(String, 1, "="))) = Trim(StringField(String, 2, "="))
....
Debug exVar("abc")
; will show
[content from file]
Re: Match variable name to string ?
Maybe Runtime can help you
Code: Select all
Define x.i
Runtime x
SetRuntimeInteger("x", 256)
Debug x
sorry for my bad english
Re: Match variable name to string ?
That is exactly what Preference file does. You then have ExaminePreferenceKeys() and NextPreferenceKey() to loop through all variables.captain_skank wrote:but i want to store the variable name and variable value ( on the same line ) in a file.
Read manual and check example: https://www.purebasic.com/documentation ... index.html