youtuber Posted April 20, 2021 Share Posted April 20, 2021 How should the stack variable $StackVariable1 and $StackVariable2 be positioned in the loop? Which is correct to add stack variables inside or outside the loop? #include <WinAPI.au3> Local $Array[5] = [1, 2, 3, 4, 5] While 1 Local $StackVariable1 = "" Sleep(100) For $i = 0 To UBound($Array) - 1 $iRandom1 = Random(0, $Array[$i], 1) If StringInStr($iRandom1, "3") Then $StackVariable1 &= $iRandom1 & @CRLF ConsoleWrite("$StackVariable1 : " & $StackVariable1 & @CRLF) EndIf Next While 2 Local $StackVariable2 = "" Sleep(200) For $i2 = 0 To UBound($Array) - 1 $iRandom2 = Random(0, $Array[$i2], 1) If StringInStr($iRandom2, "4") Then $StackVariable2 &= $iRandom2 & @CRLF ConsoleWrite("$StackVariable2 : " & $StackVariable2 & @CRLF) EndIf Next If $StackVariable1 <> $StackVariable2 Then If Not $StackVariable2 = "" Then _WinAPI_MessageBeep(5) Sleep(2000) TrayTip("$StackVariable2", $StackVariable2, 30, 1) Sleep(2000) ExitLoop EndIf EndIf WEnd WEnd or #include <WinAPI.au3> Local $Array[5] = [1, 2, 3, 4, 5] Local $StackVariable1 = "" Local $StackVariable2 = "" While 1 Sleep(100) For $i = 0 To UBound($Array) - 1 $iRandom1 = Random(0, $Array[$i], 1) If StringInStr($iRandom1, "3") Then $StackVariable1 &= $iRandom1 & @CRLF ConsoleWrite("$StackVariable1 : " & $StackVariable1 & @CRLF) EndIf Next While 2 Sleep(200) For $i2 = 0 To UBound($Array) - 1 $iRandom2 = Random(0, $Array[$i2], 1) If StringInStr($iRandom2, "4") Then $StackVariable2 &= $iRandom2 & @CRLF ConsoleWrite("$StackVariable2 : " & $StackVariable2 & @CRLF) EndIf Next If $StackVariable1 <> $StackVariable2 Then If Not $StackVariable2 = "" Then _WinAPI_MessageBeep(5) Sleep(2000) TrayTip("$StackVariable2", $StackVariable2, 30, 1) Sleep(2000) ExitLoop EndIf EndIf WEnd WEnd Link to comment Share on other sites More sharing options...
JockoDundee Posted April 20, 2021 Share Posted April 20, 2021 I’ll usually let the interpreter handle the stack variables at runtime. But it’s rare to see a program which requires the use of Random, aside from testing or gaming. What is it doing in this case? Btw, take a look at SRandom() in case your results are repeating. Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
pseakins Posted April 20, 2021 Share Posted April 20, 2021 (edited) I am not sure what you are trying to achieve with your "StackVariable"s as this is purely a programmatic concept. You can do it any way you want. However, there is no exit from either of your examples as "While 1" forms an endless loop. For a permanent loop the value following "While" can be anything at all, so long as it is not 0 or null. So, While "fred" would work equally well. Edit: In your second example the variables are being concatenated within an endless loop so eventually the code will reach the memory limit and crash. Edited April 20, 2021 by pseakins Phil Seakins Link to comment Share on other sites More sharing options...
JockoDundee Posted April 20, 2021 Share Posted April 20, 2021 7 minutes ago, pseakins said: However, there is no exit from either of your examples as "While 1" forms an endless loop. In the case of example 2, it should exit, albeit ungracefully, after much sleeping and beeping, due to the unconstrained variable concatenation. Could take a while, though. It will, most ironically, crash it’s own stack variables into the real stack Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now