Sign in to follow this  
Followers 0
superklamer

Variable outside function

6 posts in this topic

Hello,

I know this has been discussed already, but I just couldn't find the answer I was looking for. 

I have a function, that is reading a text file and outputs it to variable. How can I make that variable visible outside the function?

Local $TimeFile = ("test.txt")
FileOpen($TimeFile, 0)
Local $READTIMEFILE = FileReadLine($TimeFile)
FileClose($TimeFile)
Local $TimeRead = StringSplit($READTIMEFILE, ";")
For $i = 1 To $TimeRead[0]
   $VARIABLE = $TimeRead[2]
Next

How do I get the $VARIABLE outside my function? It will change throughout the script and I need to call this function a lot. I don't want to make this Global variable as it's constantly changing. Do I do a Dim? But how do I get it out of the function so the rest of the program can access it?

 

Thanks so much for the help in advance!!

Share this post


Link to post
Share on other sites



Use a Global variable instead of a local one or Return the value from the function.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

You may be confusing Global with a static unchanging value, which is not the case.

Most people use either Global or the Return command, as SmOke_N said.

You can also pass things as parameters.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Thanks a lot guys!!!

I used the Return and my functions is working great. 

My Code looks like this now:

Func TestFunc()
   Local $TimeFile = ("last.txt")
   FileOpen($TimeFile, 0)
   Local $READTIMEFILE = FileReadLine($TimeFile)
   FileClose($TimeFile)
   Local $TimeRead = StringSplit($READTIMEFILE, ";")
   For $i = 1 To $TimeRead[0]
      $VARIABLE= $TimeRead[2]
   Return $VARIABLE
Next
EndFunc

Just out of curiosity how would that look if I want to use a Global variable?

Share this post


Link to post
Share on other sites

Good job.

FYI, your function loops for no reason, you could just return $TimeRead[2] (if there are enough allocated dimensions).  $VARIABLE never changes the way you have it.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You are right! That's even easier.

Sometimes all you need is just a little push in the right direction  :geek:

Func TestVar()
   Local $TimeFile = ("last.txt")
   FileOpen($TimeFile, 0)
   Local $READTIMEFILE = FileReadLine($TimeFile)
   FileClose($TimeFile)
   Local $TimeRead = StringSplit($READTIMEFILE, ";")
   Return $TimeRead[2]
Next
EndFunc

Thank you SmOke_N and TheSaint!

Edited by superklamer

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  
Followers 0