Jump to content
Sign in to follow this  
Smorg

Problem declaring variables extracted from Assign()

Recommended Posts

Smorg

I'm trying to create a function that will read the entire contents of an ini file, and declare each key as a corresponding variable name, giving each variable the key's matching value.

IMO, the cleanest way to do this would be to use IniReadSection to generate an array, and then use Assign in a loop to declare the vars. The thing is that for some weird reason, it seems Assign creates some funky datatype that is only accessible using Eval. Here are some test scripts:

This one works like a charm:

$AllVars = IniReadSection("config.ini", "oog")

For $n = 1 To ($AllVars[0][0] - 1)
    Assign($AllVars[$n][0], $AllVars[$n][1])
Next

For $n = 1 To ($AllVars[0][0] - 1)
    MsgBox(0, "", $AllVars[$n][0] & " = " & Eval($AllVars[$n][0]))
NextoÝ÷ ØÚ0+zZqëaxKÚ)íé²Æ yº1Â+ajX­µêÚö«¦åzv¦zØZµ!ÎP°áj÷yéÝyÉZ­ç[È, ÈëmꮢÛÚ®&èZ²z-mç§uç%j·ªê-zºè­«­¢+ØÀÌØí±±YÉÌô%¹¥IMÑ¥½¸ ÅÕ½Ðí½¹¥¹¥¹¤ÅÕ½Ðì°ÅÕ½Ðí½½ÅÕ½Ðì¤()½ÈÀÌØí¸ôÄQ¼ ÀÌØí±±YÉÍlÁulÁt´Ä¤(%ÍÍ¥¸ ÀÌØí±±YÉÍlÀÌØí¹ulÁt°ÀÌØí±±YÉÍlÀÌØí¹ulÅt¤)9áÐ()5Í  ½à À°ÅÕ½ÐìÅÕ½Ðì°ÀÌØíYI%  1}95}I=5}%9%}%1¤oÝ÷ ÙhZ¶É¨­á1¦XÙruç%j¸§Ø^½ªâi¹^jÛazÚ)¡ûazÇ+méæjG¬Ü(®Gè®ØZ¶Ê^r'ârö«¦åy¨v®¶­sdFÒb33cµd$$ÄUôäÔUôe$ôÕôäôdÄP¢b33c´ÆÅf'2Òæ&VE6V7FöâgV÷C¶6öæfræægV÷C²ÂgV÷C¶öörgV÷C² ¤f÷"b33c¶âÒFòb33c´ÆÅf'5³Õ³ÒÒ 76vâb33c´ÆÅf'5²b33c¶åÕ³ÒÂb33c´ÆÅf'5²b33c¶åÕ³Ò¤æW@ ¤×6t&÷ÂgV÷C²gV÷C²Âb33cµd$$ÄUôäÔUôe$ôÕôäôdÄRoÝ÷ ØÚ0êÞÌ!jÛ+y©eÉû±Ò½ÝN¼¢hvaȧiܧjhÁ¬ÉתârÚ®&çë¢kayªëk'hzÉ÷öÜ(®F j)ÛyÆ®±æ®¶­Â÷öÙ^·*.)²ÚâajØZ±·KÚç_®ªëk-÷jëh×6$AllVars = IniReadSection("config.ini", "oog")

For $n = 1 To ($AllVars[0][0] - 1)
     Dim Eval($AllVars[$n][0])
    Assign($AllVars[$n][0], $AllVars[$n][1])
Next

MsgBox(0, "", $VARIABLE_NAME_FROM_INI_FILE)

Can anyone think of a way around this? The only solution I can think of is abandoning this method and using a much dirtier FileRead with some string logic to try to parse the file somehow...

Thanks in advance.

Share this post


Link to post
Share on other sites
evilertoaster

Now, If I replace the Eval in the message box...

nah... the help file says-

If there is a need to use Eval() to to read a variable, the in most situations Assign() should be used to create/write to the variable and IsDeclared() should be used to check that the variable exists.

Why are you taking the eval out when it's required?

Share this post


Link to post
Share on other sites
Smorg

Because, The strings stored in the array somehow need to be declared in a way so that they are accessible by all functions in my (very big) script. The only way to know what variable names need to be declared is by reading the key names from the INI file. Since these keys are stored in the array, the only way to declare them is by using Eval. I was under the impression that Assign would automatically declare these variables, and it does, sort of... but then these can only be actually accessed by using Eval. I need them to be normal vars somehow. (in any scope)

Share this post


Link to post
Share on other sites
evilertoaster

I need them to be normal vars somehow. (in any scope)

Then, when you use assign do it this way-

Assign($AllVars[$n][0], $AllVars[$n][1],2)

Share this post


Link to post
Share on other sites
Smorg

Then, when you use assign do it this way-

Assign($AllVars[$n][0], $AllVars[$n][1],2)
Tried it, doesn't do anything to actually create the variable.

Maybe I should rephrase my question...

Is there a way to declare a variable whose name is stored as a string inside another variable? an array in this case.

Edited by Smorg

Share this post


Link to post
Share on other sites
evilertoaster

didn't you already do that yourself in the first example?

eg-

dim $array[1][1]
$array[0][0]="name"
Assign($array[0][0],"22")
MsgBox(0,"",Eval("name"))

Share this post


Link to post
Share on other sites
Smorg

No. Maybe i can't use assign then because whats happening is I have a big script with lots of functions already completed. I previously had all variables declared directly in the script, but now want to create a user configuration file so i can distribute a compiled version of the script. I cannot convert everything to use Eval... that would be extremely ugly, and probably not possible. I suppose I could add like 700 lines of code and iniread everything individually. ^^

Share this post


Link to post
Share on other sites
smashly

What functions don't work with using an Array element as the variable?

Why assign/eval something that already assigned and evaluated?

$AllVars = IniReadSection("config.ini", "oog") ;<- This creates a 2D array

For $n = 1 To $AllVars[0][0]
    MsgBox(0, "", $AllVars[$n][0] & " = " & $AllVars[$n][1]) ;<--  are already assigned by IniReadSetction.
Next

Why eval when it's already a clear cut value?

Sorry for my ignorance but I can't see the problem in using the already created array elements as the variables.

I must be missing something when I see what your asking.

Edited by smashly

Share this post


Link to post
Share on other sites
McGod

If FileExists(@ScriptDir & "/Test.ini") = 0 Then
    IniWrite(@ScriptDir & "/Test.ini", "Global", "Vari1", "Test")
    IniWrite(@ScriptDir & "/Test.ini", "Global", "Vari2", "ing")
    IniWrite(@ScriptDir & "/Test.ini", "Global", "Vari3", "Script")
EndIf

Global $Read = IniReadSection(@ScriptDir & "/Test.ini", "Global")
If @error Then Exit

For $i = 1 To $Read[0][0] Step 1
    Assign($Read[$i][0], $Read[$i][1])
Next

MsgBox(1, "Test", $Vari1 & $Vari2 & " " & $Vari3)

This works fine, lemme test yurs.

Smorg: ($AllVars[0][0] - 1) To $AllVars[0][0]

Your not actually looping it since Im Guessing your ini only has the one.

Edited by Chip

Share this post


Link to post
Share on other sites
evilertoaster

so you want to use an .ini configuration file to inport some data into your script?

that has nothing to do with why you need to rename the variables.... you just want to change thier values...

i tipcical example is somthing like this at the start of your code that assigns variables from an ini file if they exist-

$FavoriteColor = IniRead("Config.ini","User","FavColor","Blue")
$Age = IniRead("Config.ini","User","Age","18")
$Name = IniRead("Config.ini","User","Name","Blue")
$Port = IniRead("Config.ini","Network","Port","1123")
$IP = IniRead("Config.ini","Network","IP","127.0.0.1")

Share this post


Link to post
Share on other sites
McGod

Smorg: You have another irc u go on other then mmBot's? I cannot go on it due to my ISP being douches.

Share this post


Link to post
Share on other sites
McGod

Smorgs problem is in his loop:

$AllVars = IniReadSection("config.ini", "oog")

For $n = 1 To ($AllVars[0][0] - 1)
    Assign($AllVars[$n][0], $AllVars[$n][1])
Next

MsgBox(0, "", $VARIABLE_NAME_FROM_INI_FILE)
oÝ÷ ÛMúYUj»4ÓSjx§]

works.

Share this post


Link to post
Share on other sites
Smorg

Smorgs problem is in his loop:

$AllVars = IniReadSection("config.ini", "oog")

For $n = 1 To ($AllVars[0][0] - 1)
    Assign($AllVars[$n][0], $AllVars[$n][1])
Next

MsgBox(0, "", $VARIABLE_NAME_FROM_INI_FILE)
oÝ÷ ÛMúYUj»4ÓSjx§]

works.

That works for you?? Thats really weird because it causes a bunch of au3 wrapper errors about the vars not being declared, but i noticed if I ignore it, it will still work. But the problem couldn't be the -1. Thats needed because $allVars[0][0] returns the total elements. Since our loop is starting at 1 we can't loop the last time, and its still looping without error so thats not it.

For some reason when I integrate this into my actual script it won't work though... i'll have to do some more testing.

so you want to use an .ini configuration file to inport some data into your script?

that has nothing to do with why you need to rename the variables.... you just want to change thier values...

i tipcical example is somthing like this at the start of your code that assigns variables from an ini file if they exist-

Yes I know... I just want to do it algorithmically because the inifile is just so damn big. Also makes adding vars very simple as i'm developing since theres no need to add another iniread line for every ini line I add.

What functions don't work with using an Array element as the variable?

Why assign/eval something that already assigned and evaluated?

CODE: AutoIt

$AllVars = IniReadSection("config.ini", "oog") ;<- This creates a 2D array

For $n = 1 To $AllVars[0][0]

MsgBox(0, "", $AllVars[$n][0] & " = " & $AllVars[$n][1]) ;<-- are already assigned by IniReadSetction.

Next

Why eval when it's already a clear cut value?

Sorry for my ignorance but I can't see the problem in using the already created array elements as the variables.

I must be missing something when I see what your asking.

Because that would be the equivalent of naming your vars $a, $b, $c, $d.......

Smorg: You have another irc u go on other then mmBot's? I cannot go on it due to my ISP being douches.

Ahh you can't get on irc either? You got aim? otherwise freenode or oftc. Edited by Smorg

Share this post


Link to post
Share on other sites
McGod

au3 wrapper errors are because it parses it and doesn't physically see the variable.

Share this post


Link to post
Share on other sites
Smorg

Forgot to mention. You can try forcing mmbot to resolve to nico's IP.

if your having a problem still then add in your hosts file:

mmbot.net 72.232.33.235

until it is fixed that is.

Share this post


Link to post
Share on other sites
Smorg

Found the problem. Since autoit detects all of these not delcared errors, I can't put my code into a function within an include file, because even if the iniload function is called before any of the vars are used, autoit will still give a fatal error.

Fixed by putting this code before all #includes. This is somewhat of a bug on autoit's part since i can't run this from a function to reuse the code later (unless i duplicate it)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×