diff Posted September 23, 2021 Share Posted September 23, 2021 Hello, feeling dumb that I can't resolve my issue, my point is to not use Global variables in bigger projects at all. So I have 2 functions, the first is main function and the second searching for information from websites and attaches the extracted information to variables. Func main() ...does some code... ..then later i have some code which need to use variables from web_extraction() function.. example: StringStripWS($var1, 3) Of course I get here error that variable $var1 possibly not declared EndFunc Func web_extraction() ..does some code... ...extracts information from websites.. $var1 = "extracted info 1" $var2 = "extracted info 2" etc etc. EndFunc Of course I can write at the beginning Global $var1, $var2 and this will work, but how I can use $var1, $var2 which kill itself after function ends in main function? I have read a lot of information about $params in function and etc. tried everything and still receive that the variables are possibly not declared and just stay blank inside. Any examples how correctly to write this? Leendert-Jan 1 Link to comment Share on other sites More sharing options...
Alecsis1 Posted September 23, 2021 Share Posted September 23, 2021 Hello! Try something like this: Func web_extraction($sToDo) ; $sToDo: 'Extract' = extracts information from websites.. ; $sToDo: 'GetInfo' = return information collected before ; Local Static $var[362] ; for example, 362 items If $sTodo = 'Extract' Then ..does some code... ...extracts information from websites.. and fills the array with info $var[0] = "extracted info 1" $var[0] = "extracted info 2" ... etc ... Return EndIf If $sTodo = 'GetInfo' Then ...does some code if needed Return $var EndIf EndFunc Link to comment Share on other sites More sharing options...
junkew Posted September 25, 2021 Share Posted September 25, 2021 You could work with class object solutions. See for example FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Danyfirex Posted September 25, 2021 Share Posted September 25, 2021 Hello, Maybe this: _Test() Func _Test() _1() _2() _3() EndFunc ;==>_Test Func _1() ConsoleWrite("_1 Set MyData To 'Hello World'" & @CRLF) _MyData("Hello World") EndFunc ;==>_1 Func _2() ConsoleWrite("_2 MyData Value: " & _MyData() & @CRLF) ;~ _MyData("") ;Clear _MyData(_MyData() & " - AutoIt Rocks!!!") ;Append Data EndFunc ;==>_2 Func _3() ConsoleWrite("_3 MyData Value: " & _MyData() & @CRLF) EndFunc ;==>_2 Func _MyData($Data = Default) Local Static $MyData = "" If @NumParams = 0 Then Return $MyData $MyData = $Data EndFunc ;==>_MyData Saludos junkew 1 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
junkew Posted September 26, 2021 Share Posted September 26, 2021 @Danyfirex Interesting way of using static expandcollapse popup_ClassTest() Func _ClassTest() _1() _2() _3() EndFunc ;==>_Test Func _1() ConsoleWrite("Creating new class myclass" & @CRLF) _MyClass("Hello World") EndFunc ;==>_1 Func _2() ConsoleWrite("_2 MyData Value: " & _MyClass("Property2") & @CRLF) ;~ _MyData("") ;Clear _MyClass("Property2",_MyClass("Property2") & " - AutoIt Rocks!!!") ;Append Data EndFunc ;==>_2 Func _3() ConsoleWrite("_3 MyClass data: " & _MyClass() & @CRLF) _MyClass("ha") EndFunc ;==>_2 func ha() consolewrite("Hello AutoIt") endfunc Func _MyClass($propName = Default, $propData=Default) Local Static $Property1 = " a " Local Static $Property2 = " b " Local Static $Property3 = " c " Local Static $Property4 = " d " local static $method1 = ha() If @NumParams = 0 Then Return "{" & $property1 & ";" & $property2 & ";" & $property3 & ";" & $property4 & "}" if @NumParams = 1 then if isdeclared($propname) then return eval($propName) Else $property1=$propname EndIf EndIf If @NumParams = 2 then if isdeclared($propname) then assign($propname,$propdata) endif EndIf EndFunc ;==>_MyClass Danyfirex 1 FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
JockoDundee Posted September 26, 2021 Share Posted September 26, 2021 Looking back at the initial problem posted by the OP, I don’t think it takes much more than: Func main() Local $var1, $var2 web_extraction($var1, $var2) StringStripWS($var1, 3) EndFunc Func web_extraction(ByRef $var1, ByRef $var2) $var1 = "extracted info 1" $var2 = "extracted info 2" EndFunc seadoggie01, mikell, Danyfirex and 1 other 4 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