Match variable name to string ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Match variable name to string ?

Post by captain_skank »

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
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Match variable name to string ?

Post by Marc56us »

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())
:wink:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Match variable name to string ?

Post by wilbert »

Maybe use a Map ?
Windows (x64)
Raspberry Pi OS (Arm64)
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Match variable name to string ?

Post by Marc56us »

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)
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Match variable name to string ?

Post by captain_skank »

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 :

Code: Select all

abc = [content from file]
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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Match variable name to string ?

Post by Dude »

I don't understand what's being asked. Can you explain it differently?
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Match variable name to string ?

Post by Bisonte »

Code: Select all

abc = [content from file]
Only with maps.

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]
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Match variable name to string ?

Post by Josh »

Maybe Runtime can help you

Code: Select all

Define x.i
Runtime x

SetRuntimeInteger("x", 256)
Debug x
sorry for my bad english
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Match variable name to string ?

Post by cas »

captain_skank wrote:but i want to store the variable name and variable value ( on the same line ) in a file.
That is exactly what Preference file does. You then have ExaminePreferenceKeys() and NextPreferenceKey() to loop through all variables.
Read manual and check example: https://www.purebasic.com/documentation ... index.html
Post Reply