Jump to content
Sign in to follow this  
butterfly

About Global Local and For next $z help me tutorial

Recommended Posts

butterfly

Hello FellowForumers

I have some questions about this subject and Although I have researched about i have some problems let it sink in proper

Although i have basic understanding of Global and Local I Still have questions to be absolute sure:

#include <Array.au3>

Global $TSAB[20]

While 1
    trading()
    If $TSAB[19] = True Then MsgBox(0,"","Thank you AutoIT Forum ^^ ")
;~  If $TSAB[19] = True then Exit
WEnd

func trading()
    $TSAB[19] = ""
    $TSAB[7] = ""
    while 1
        _ArrayDisplay($TSAB)
        $TSAB[7] = "done"
        _ArrayDisplay($TSAB)
        If $TSAB[7] = "Done" Then $TSAB = ClientDoneCommand()
        _ArrayDisplay($TSAB)
        If $TSAB[19] = True then Return $TSAB
    WEnd
EndFunc

func ClientDoneCommand()
    $TSAB[19] = True
    Return $TSAB
EndFunc

My own example Above gives to me Decent understanding that Global can be called anywhere in the program and that the global variable is sent and called towards other functions and loops as it was just before it got sent or returned

is there anything I have to keep in mind to Prevent any big mess? Am I something missing about this or has anyone some advice?

I will use this global as the soul of my program and store information on where it has been and been and what functions it has visit and what it has been doing etc

Here I move to Local $var

So I got the same script and added some Local var:

#include <Array.au3>

Global $TSAB[20]
local $hugs = ""

While 1
    ConsoleWrite($hugs & "0" & @LF) ;Added line
    $hugs = trading()
    If $TSAB[19] = True Then MsgBox(0,"","Thank you AutoIT Forum ^^ ")
;~     If $TSAB[19] = True then Exit
ConsoleWrite($hugs & "1" & @LF) ;Added line
WEnd

func trading()
    $TSAB[19] = ""
    $TSAB[7] = ""
    local $hugs = "yes"
    while 1
        ConsoleWrite($hugs & "2" & @LF) ;Added line
        _ArrayDisplay($TSAB)
        $TSAB[7] = "done"
        _ArrayDisplay($TSAB)
        If $TSAB[7] = "Done" Then $TSAB = ClientDoneCommand($hugs)
        _ArrayDisplay($TSAB)
        ConsoleWrite($hugs & "4" & @LF) ;Added line
        If $TSAB[19] = True then Return $TSAB
    WEnd
EndFunc

func ClientDoneCommand($hugs)
    $TSAB[19] = True
    ConsoleWrite($hugs & "3" & @LF) ;Added line
    $hugs = "Maybe"
    Return $TSAB
EndFunc

Here I added a few console writes and numbered them the consolewrite output is

0
yes2
yes3
yes4
1
repeat

So the following things I see in my blurry mind as are as follow

at the very start I need to declare Local $hugs = ""  else ;
ConsoleWrite($hugs & "0" & @LF) 

 ;Added line will have variable used before declaration (meaning nothing has been stored ever which I understand)

Now here comes an interesting part to me, about:

    $hugs = trading()
    If $TSAB[19] = True Then MsgBox(0,"","Thank you AutoIT Forum ^^ ")
;~     If $TSAB[19] = True then Exit
ConsoleWrite($hugs & "1" & @LF) ;Added line

here Happens nothing , not even a error (which I expected)

I do not get a consolewrite: 0

it does not output anything

I understand that the functions has not returned the variable of

local $hugs = "yes" or     $hugs = "Maybe"

But I expected an error, why no error?

What Happens with the at line 33

$hugs = "Maybe"

does it get lost? deleted (from the memory ) at the end of the function?

@ line 17?

local $hugs = "yes"

Would it be wise to write there local $hugs = "yes" or is $hugs = "yes" Good enough , does it really matter with managing anything?

and as last I also understand if I would change

that at line 26 that it is not needed the write return $TSAB but just return would be good enough?

because $TSAB is in every function / If / while for next loop avaiable and it will carry over the information stored inside of it?

if i would change it into

Return $hugs

then

ConsoleWrite($hugs & "1" & @LF)  will give me yes1

and even at the very start of the while loop

ConsoleWrite($hugs & "0" & @LF)   will give me yes0 and it was previous just 0

so it seems

anything wrong here? so it seems the local does not get lost at the end of the while loop

then my final questions about for / next

For $z = 0 to 9
    For $x = 0 to 9
        For $y = 0 to 9
        ConsoleWrite($z & $x & $y & @LF)
        next
    next
next
if $z = 9 Then  MsgBox(0,"","1000 ^^ ")
if $x = 9 Then  MsgBox(0,"","x = 9 ^^ ")
if $y = 9 Then  MsgBox(0,"","y = 9 ^^ ")

the Variable $z , $x and $y

are they only used in the for next loop or can they be used outside of it?

my msgboxs do not work here after the next so i guess they end right away in the for next loop

any tips or suggestions are welcome if i am saying anything wrong or retarded here

Cheers Butterfly

Share this post


Link to post
Share on other sites
water

tl;dr

All you need to know about Global/Local is described in the Help file or in the Wiki.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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  

  • Similar Content

    • ShakeelAhmad
      By ShakeelAhmad
      I've to upload different number of photos online in different assignments. There is a problem that all photos cannot be selected at once to upload. I have to choose and upload photos one by one. For this purpose I have made a script to automate whatever I have to do manually for choosing and uploading photos one by one.
      Please see my script below and check the last "MouseClick" command. This command clicks the button to choose and upload next photo. The problem I'm facing is; the last "MouseClick" works 1 step extra when all photos have been selected and uploaded. I mean if 7 photos are to be uploaded, this command opens the box from where next photo is selected and uploaded then it again opens the box and next photo is choosen and so on.... when last photo is selected and uploaded, this button once again opens the box. When all photos are uploaded, it should not click the button to select next photo. Please suggest how can I resolve this issue.
      #include <AutoItConstants.au3> Sleep(200) HotKeySet("{ESC}","Quit") ;Press ESC key to quit Send("{ALT DOWN}") Send("{TAB}") Send("{ALT UP}") Sleep(200) Local $photos = InputBox("Question", "How many photos to upload?", "#", "", _ - 1, -1, 0, 0) ; How many photos to upload Local $selector = 0 While $photos <> $selector MouseClick("Left", 281, 238, 1) ; mouse click on very first photo in the box. if $selector = 0 Then Send("{ENTER}") ; for selecting very first photo from "open" window. Sleep(800) Else sleep(200) Send("{RIGHT " & $selector & "}") ; for selecting 2nd to onward photos from "open" window. sleep(1000) Send("{ENTER}") sleep(1000) EndIf MouseClick("Left", 495, 198, 1) ; for clicking a button to choose next photo to upload $selector = $selector + 1 WEnd Beep(1500, 300) ; beep when all photos uploaded Exit Regards,
       
      Shakeel
    • Dzenan03
      By Dzenan03
      I want to make a while loop, that creates variables based on a array. For thist I created the array $iDsO with the number and the name of folders in an other folder. Every folder has a different name an I want to create variables(arrays) for each folder that show me all the files in that folder. For example: I have the Folder \Folder1. In it there are the Folders \1, \2, \3. In 1, 2 and 3 there are some files(.png). The array for Folder1 is $iDsO and now I want to crate the arrays $iDsO1, $iDsO2 and $iDsO3 with the files in them can I make something like this:
      While $iDs > 0 ;$iDs is the number of files in Folder1>> $iDsO[0] $iDs#here should come the Foldername for example '1'# = _FileListtoArray(@ProgramFilesDir&"\Folder1\"&$iDsO[$iDs]) $iDs = $iDs - 1 Wend So that in the End I have three variabels ($iDs1, $iDs2 and $iDs3)
       
      Is this posible or if not what could I do instead ( I don´t know the number of folders in Folder1 in the begining).
    • nacerbaaziz
      By nacerbaaziz
      hi dears, i have an question please if you can help me.
      i maked an program to do some functions
      and this program works with the global hot keys
      i added an option to the users to change the hot keys
      when i searched about how to do that i found this UDF
      GUIHotkey.au3
      http://www.autoitscript.com/forum/index.php?showtopic=107965
      but the problem here that this UDF cant be detect the win key.
      can you help me to add it, or if there are any others UDFSS wich do that work please give me some.
      i hope that my question is clear for you.
      thanks in advance
    • California
      By California
      Hello,
      I wrote a benchmark script to measure variable declarations
      to find out whether you should focus more on static or global variables
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 #ce ---------------------------------------------------------------------------- #Region Pre-Setting Local $iTally1 = 0 Local $iTally2 = 0 Local $iTally3 = 0 Local $iTally4 = 0 Local $iTally5 = 0 Local $iTally6 = 0 Local $iTally7 = 0 Global $GLOBALCONST1 = 1 Global $GLOBALCONST2 = 1 Global $GLOBALCONST3 = 1 Global $GLOBALCONST4 = 1 Global $GLOBALCONST5 = 1 #EndRegion Pre-Setting #Region Test Functions Func s1() Static $i = $GLOBALCONST1 Return $i EndFunc Func g2() Return $GLOBALCONST2 EndFunc Func g3() Static $i7 = "gsdgdfegbgbrwefw" Return $GLOBALCONST3 EndFunc Func g4() Static $i1 = 1 Static $i2 = "asd" Static $i3 = 234 Static $i4 = True Static $i5 = [0] Static $i6 = "hgsdg" Static $i7 = 1 Static $i8 = 1 Static $i9 = 1 Static $i0 = 1 Return $GLOBALCONST4 EndFunc Func g5() Local $i = $GLOBALCONST5 Return $i EndFunc Func g6() Local $i = 1 Return $i EndFunc Func g7() Return 1 EndFunc #EndRegion Test Functions #Region Benchmark Loop For $i = 0 To 15 Local $tDelta = TimerInit() Do $iTally1 += s1() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally2 += g2() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally3 += g3() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally4 += g4() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally5 += g5() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally6 += g6() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally7 += g7() Until TimerDiff($tDelta) >= 1000 Next #EndRegion Benchmark Loop ConsoleWrite(@CRLF&"Static1: "&$iTally1&" pkt"&@CRLF&"Global2: "&$iTally2&" pkt"&@CRLF&"Global3: "&$iTally3&" pkt"&@CRLF&"Global4: "&$iTally4&" pkt"&@CRLF&"Local5: "&$iTally5&" pkt"&@CRLF&"Local6: "&$iTally6&" pkt"&@CRLF&"Hardcode7:"&$iTally7&" pkt"&@CRLF) #cs Result Static1: 10291881 pkt global to static Global2: 13977324 pkt only global Global3: 9886169 pkt global and static Global4: 2933051 pkt global and many statics Local5: 9937314 pkt global to local Local6: 10306484 pkt only local Hardcode7: 14835319 pkt no variable #ce Result:
      100% no variable, hardcore value
      94% only global variable use
      69% only local variable use with hardcore value set
      69% only static variable use with global variable value set
      67% declaration of local variable with global variable value set
      66% only global variable use with one static variable beside
      20% only global variable use with ten static variables beside
      My thesis of the result:
      Be careful with declarations, whether local, global or static Note: in my test the global variable performance was better than the local one, but in practice the global one would lose performance due to multiple operations
       
      What is your best practice sharing data between multiple functions?
    • InunoTaishou
      By InunoTaishou
      I know on other languages when you try to use a string like this
      Global $sString = "Some String" If ($sString) Then ; ... EndIf The compiler will fail because string cannot be used in an expression where it needs a bool (something along those lines). But I see instances in other languages where using $sString in an expression will work and implicitly use the length of the string (or because there is some value in that variable).
      I'm wondering why AutoIt doesn't do the same. If the $sString is used like in my example (or some other expression where it wants a bool type) implicitly use the length of the string instead of the converting to 0?
×