Page 1 of 1 1
Topic Options
#184378 - 2008-01-03 12:32 AM Create variables based on ini
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Is there a way to dynamically create a variable based on the contents of an ini file?

I have a script that pulls info from an ini file and everything works fine. What I would like to do is add a section to the ini, and when that section is read, create a variable for each key in the section using the keyname as the variable name and the keyvalue as the variable value. Is this possible? I would like for the variables to be available for use in the entire script.
 Code:
ini file
-------------------------------------------
[Vars]
red = some color
blue = another color
-------------------------------------------

Code:
$Keys = EnumIni($file, 'Vars')
For Each $key in $keys
  $value = ReadProfileString($file, "Vars", $key)
  ;Now I would like to create a variable using the value of $key for the var name 
  ;and the value of $value as the var value
Next

Top
#184379 - 2008-01-03 01:46 AM Re: Create variables based on ini [Re: JohnQ]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Take a look at the Execute function - it might help.

With Execute "commands", you can create a Kix command string - something like
 Code:
$varname=ReadProfileString('file.ini', 'section', 'VarName')$value=ReadProfileString('file.ini', 'section', 'Value')

$CmdString = 'Dim ' + $Varname + @CRLF + $Varname + '=' + $Value
Execute $CmdString

Of course, there's the issue of the rest of your commands knowing what the varname is, unless you always use Execute.

You might want to look at Howard Bullock's HASH udfs, which work like the Hash function in perl. It lets you reference data associatively (by name, not by varname) which might be more like what you want/need.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#184380 - 2008-01-03 02:15 AM Re: Create variables based on ini [Re: Glenn Barnas]
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Thanks Glen, that worked great! I just had to add extra $'s in the string for variable declarations.

$CmdString = 'Dim $' + $key + @CRLF + '$' + $key + '=' + $Value

Top
#184381 - 2008-01-03 03:48 AM Re: Create variables based on ini [Re: JohnQ]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
yeah, I knew that - it's all part of my policy of leaving some challenge to the original poster. ;\)

Glad it worked for you.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 764 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.059 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org