Jump to content

Setting Variables


Recommended Posts

Hi,

I'm trying to use IniReadSection from inside of one of my functions and I'm wanting to take each ini key and make it a variable that I can access throughout the script.

$var = IniReadSection("C:\Temp\myfile.ini", "section2")
If @error Then 
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $var[0][0]
        MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
    Next
EndIf

Now, obviously "$var[$i][0]" is the key I'm after and I believe I can make it global to everything by doing:

$var = IniReadSection("C:\Temp\myfile.ini", "section2")
If @error Then 
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $var[0][0]
        $var_name = $var[$i][0]
        $var_value = $var[$i][1]
        
        Global $var_name
        
        ;how to set the global $var_name value now?
    Next
EndIf

Am I on the right path here?

If so how do I set the value then?

Link to comment
Share on other sites

Just put "Global $var_name" somewhere in the main part of your script.

(usually you declare the variable first)

You can change its value from anywhere after that.

Global $var_name
$var = IniReadSection("C:\Temp\myfile.ini", "section2")
If @error Then 
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $var[0][0]
        $var_name = $var[$i][0]
        $var_value = $var[$i][1]
    Next
EndIf

Looking at what's happening at this moment inside your example script: $var_name will take all the values in your array and ending up being equal to the last one at the end of the loop; you will need to USE that somewhere before going to the next value ...

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

I'm not sure if I'm following along correctly. I'm trying to make dynamic variables from within my ini file. Is there no way to make a global variable from inside of the function and assign it's value? That way the loop sets all the global variables and their values without me having to predefine it

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Sure you can, but that don't mean you should.

He can and probably will as is valid to do so. You saw the posted help by blakel? :(

Assign("MyVar", "MyData", 2)
MyFunction1()
MyFunction2()

func MyFunction1()
    if Eval("MyVar") Then
        MsgBox(0, '', Eval("MyVar"))
        ;; other code.
    EndIf
EndFunc

func MyFunction2()
    Assign("MyVar", 1, 2)
EndFunc

It seems like doing magic :)

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...