Jump to content

assign function


Recommended Posts

Hi !

First excuse me for my english :">

I've some problems with the assign function. I want import data from sections of my ini file (5 lines by sections : dir, file, name, process, run). This part of code returns good values in the message box.

But if i want to use my variables created by the import function after the importation, variables are empty... :) (for exemple try to display the message box again after "next")

CODE
For $n = 1 to $nbfiles

import()

next

Func import()

$var = IniReadSection($ini, $n)

For $i = 1 To $var[0][0]

If $var[$i][0] == "dir" Then

Assign("dir" & $n, $var[$i][1])

ElseIf $var[$i][0] == "file" Then

Assign("file" & $n, $var[$i][1])

ElseIf $var[$i][0] == "name" Then

Assign("name" & $n, $var[$i][1])

ElseIf $var[$i][0] == "process" Then

Assign("process" & $n, $var[$i][1])

ElseIf $var[$i][0] == "run" Then

Assign("run" & $n, $var[$i][1])

EndIf

Next

MsgBox (4096, "IMPORT", "dir : " & Eval("dir" & $n) & " file : " & Eval("file" & $n) & " name: " & Eval("name" & $n) & " process : " & Eval("process" & $n) & " run : " & Eval("run" & $n))

EndFunc

The aim is to create dir1, dir2... file1, file2... etc and use them in another function.

thank you for helping.

TaGaDa

Link to comment
Share on other sites

dude by your code the '$n' var is never transmitted to the function thus (unless it's on the global scope) it will cause the Assign function to fail since the variable is empty in there! try passing it along with the function call:

For $n = 1 to $nbfiles
import($n)
next

Func import($n)
...
EndFunc

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Link to comment
Share on other sites

Looks to be working for me. I changed it to Switch as looks and performs better.

$ini = 'file.ini'
$nbfiles = 5

For $n = 1 To $nbfiles
    import()
Next

Func import()
    $var = IniReadSection($ini, $n)

    For $i = 1 To $var[0][0]
        Switch $var[$i][0]
            Case "dir"
                Assign("dir" & $n, $var[$i][1])
            Case "file"
                Assign("file" & $n, $var[$i][1])
            Case "name"
                Assign("name" & $n, $var[$i][1])
            Case "process"
                Assign("process" & $n, $var[$i][1])
            Case "run"
                Assign("run" & $n, $var[$i][1])
        EndSwitch
    Next

    MsgBox(4096, "IMPORT", _
            "dir : " & Eval("dir" & $n) _
            & " file : " & Eval("file" & $n) _
            & " name: " & Eval("name" & $n) _
            & " process : " & Eval("process" & $n) _
            & " run : " & Eval("run" & $n))

EndFunc   ;==>import

ini file used

[1]
dir=d1
file=f1
name=n1
process=p1
run=r1
[2]
dir=d2
file=f2
name=n2
process=p2
run=r2
[3]
dir=d3
file=f3
name=n3
process=p3
run=r3
[4]
dir=d4
file=f4
name=n4
process=p4
run=r4
[5]
dir=d5
file=f5
name=n5
process=p5
run=r5

:)

Link to comment
Share on other sites

indeed it works ! My part of code too... but, after if you try this

$ini = 'file.ini'
$nbfiles = 5

For $n = 1 To $nbfiles
    import()
Next

For $n = 1 To $nbfiles
    test()
Next

Func import()
    $var = IniReadSection($ini, $n)

    For $i = 1 To $var[0][0]
        Switch $var[$i][0]
            Case "dir"
                Assign("dir" & $n, $var[$i][1])
            Case "file"
                Assign("file" & $n, $var[$i][1])
            Case "name"
                Assign("name" & $n, $var[$i][1])
            Case "process"
                Assign("process" & $n, $var[$i][1])
            Case "run"
                Assign("run" & $n, $var[$i][1])
        EndSwitch
    Next

    MsgBox(4096, "IMPORT", _
            "dir : " & Eval("dir" & $n) _
            & " file : " & Eval("file" & $n) _
            & " name: " & Eval("name" & $n) _
            & " process : " & Eval("process" & $n) _
            & " run : " & Eval("run" & $n))

EndFunc   ;==>import

Func test()
    MsgBox(4096, "IMPORT", _
            "dir : " & Eval("dir" & $n) _
            & " file : " & Eval("file" & $n) _
            & " name: " & Eval("name" & $n) _
            & " process : " & Eval("process" & $n) _
            & " run : " & Eval("run" & $n))
EndFunc

As you can see the test function shows that variable are empty :

msg : "dir : file : name: process : run : "

I do not understand why...

Have you an idea ?

Edited by tagada
Link to comment
Share on other sites

I do not understand why...

Have you an idea ?

The reason is because Assign() is creating the variables in a Local scope and when the function returns then they are destroyed. You need to use the 3rd parameter of Assign() to force Global scope.

$ini = 'file.ini'
$nbfiles = 5

For $n = 1 To $nbfiles
    import()
Next

For $n = 1 To $nbfiles
    test()
Next

Func import()
    $var = IniReadSection($ini, $n)

    For $i = 1 To $var[0][0]
        Switch $var[$i][0]
            Case "dir"
                Assign("dir" & $n, $var[$i][1], 2)
            Case "file"
                Assign("file" & $n, $var[$i][1], 2)
            Case "name"
                Assign("name" & $n, $var[$i][1], 2)
            Case "process"
                Assign("process" & $n, $var[$i][1], 2)
            Case "run"
                Assign("run" & $n, $var[$i][1], 2)
        EndSwitch
    Next

EndFunc   ;==>import

Func test()
    MsgBox(4096, "IMPORT", _
            "dir : " & Eval("dir" & $n) _
            & " file : " & Eval("file" & $n) _
            & " name: " & Eval("name" & $n) _
            & " process : " & Eval("process" & $n) _
            & " run : " & Eval("run" & $n))
EndFunc

Works better this time

:)

Link to comment
Share on other sites

Ho ! Ok. I read the help file, but i am not sure to understand very well the differences between Global & Local

Yeah it works as i want !

I'll try in my code later, now i have to leave. I'll report the result.

Thank you again.

Have a nice day.

Link to comment
Share on other sites

Yeah it works as i want !

I'll try in my code later, now i have to leave. I'll report the result.

...and then learn to use arrays and never code again with Assign()/Eval(), unless Mhz makes you do it at gunpoint!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

As promised : the result !

It works well ! I'll be able to continue now. Thank you again and again.

..and then learn to use arrays and never code again with Assign()/Eval(), unless Mhz makes you do it at gunpoint!

I thought about that... it's my new challenge now :rambo: ! I hope i'll succeed :rolleyes:

Bye all

TaGaDa.

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...