SeF Posted August 18, 2010 Share Posted August 18, 2010 Hello!Can I use variables that have been created inside functions?Like, for example:ExampleFunction() MsgBox(0, "Variable value", $Variable) Func ExampleFunction() $Variable = "Hello!" EndFuncIf I try to compile it, will appear the following messages:WARNING: $Variable: possibly used before declaration.Variable used without being declared.I know that this can fixed by just declaring the variable before using the function. But what should I do when I'm using hundreds of variables inside of various functions? Declare each one of them?Tks! Link to comment Share on other sites More sharing options...
water Posted August 18, 2010 Share Posted August 18, 2010 (edited) A variable that is not declared anywhere in the script is autom. declared when first used (or you get an error depending on the AutoIT options set). If the first usage is within a function then this variable is local to the function and can not be used outside this function. Please see "Language Reference -> Variables -> Section: Scope" for more information. BTW: Why do you use hundreds of variables? Would arrays help? Edited August 18, 2010 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
cageman Posted August 18, 2010 Share Posted August 18, 2010 if you need a lot of the variables from one function you could return it in an array, but i would first take a look at your program structure cause it sounds not to good. Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 19, 2010 Share Posted August 19, 2010 (edited) Can I use variables that have been created inside functions?You can use the Global keyword to declare global scope variables within a function. But it is bad practice, and generally indicates you haven't thought something through all the way. Like, for example: ExampleFunction() MsgBox(0, "Variable value", $Variable) Func ExampleFunction() $Variable = "Hello!" EndFuncYou would just make that: Func ExampleFunction() Global $Variable = "Hello!" EndFunc If I try to compile it, will appear the following messages: WARNING: $Variable: possibly used before declaration. Variable used without being declared.Adding the Global keyword will make it work, but you still get a warning from the syntax check, because there is no way for it to know for sure the function will be called before the variable is used. I know that this can fixed by just declaring the variable before using the function. But what should I do when I'm using hundreds of variables inside of various functions? Declare each one of them?You haven't described what your script is doing. But variables that are only used within a function don't need Global scope. When one function calls another, the info it needs should be passed in parameters, so still no Global scope required. You shouldn't need very many global variables, but the ones you do need should be declared globally early in your script and then referenced by the functions without having to re-declare them. Edited August 19, 2010 by PsaltyDS 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 More sharing options...
SeF Posted August 24, 2010 Author Share Posted August 24, 2010 Thanks for the answers! I think it's better to rewrite my script. I wasn't using array because of the names of the variables...It's easier to manipulate/change/use later. Some part from my script: expandcollapse popup#include <File.au3> #include <IE.au3> Func DadosDataFile() _FileReadToArray(@DesktopDir & "\DataFile.pdf", $DataFile) $DataFile_Regiao = $DataFile[1] $DataFile_GrupoVendedor = $DataFile[2] $DataFile_Vendedor = $DataFile[3] $DataFile_Coordenador = $DataFile[4] $DataFile_RazaoSocial = $DataFile[5] $DataFile_Fundacao = $DataFile[6] $DataFile_ClienteDesde = $DataFile[7] $DataFile_QuantidadeFuncionarios = $DataFile[8] $DataFile_NomeFantasia = $DataFile[9] $DataFile_CNPJ = $DataFile[10] $DataFile_InscricaoEstadual = $DataFile[11] $DataFile_Suframa = $DataFile[12] $DataFile_Rua = $DataFile[13] $DataFile_Numero = $DataFile[14] $DataFile_Complemento = $DataFile[15] $DataFile_CEP = $DataFile[16] $DataFile_Bairro = $DataFile[17] $DataFile_Municipio = $DataFile[18] $DataFile_UF = $DataFile[19] $DataFile_Telefone = $DataFile[20] $DataFile_Celular = $DataFile[21] $DataFile_Fax = $DataFile[22] $DataFile_email = $DataFile[23] $DataFile_Rua2 = $DataFile[24] $DataFile_Numero2 = $DataFile[25] $DataFile_Complemento2 = $DataFile[26] $DataFile_CEP2 = $DataFile[27] $DataFile_Bairro2 = $DataFile[28] $DataFile_Municipio2 = $DataFile[29] $DataFile_UF2 = $DataFile[30] $DataFile_Telefone2 = $DataFile[31] $DataFile_Celular2 = $DataFile[32] $DataFile_Fax2 = $DataFile[33] $DataFile_email2 = $DataFile[34] $DataFile_cFormaTrat1 = $DataFile[35] $DataFile_cNome1 = $DataFile[36] $DataFile_cFuncao1 = $DataFile[37] $DataFile_cNascimento1 = $DataFile[38] $DataFile_cTelefone1 = $DataFile[39] $DataFile_cCelular1 = $DataFile[40] $DataFile_cEmail1 = $DataFile[41] $DataFile_cFormaTrat2 = $DataFile[42] $DataFile_cNome2 = $DataFile[43] $DataFile_cFuncao2 = $DataFile[44] $DataFile_cNascimento2 = $DataFile[45] $DataFile_cTelefone2 = $DataFile[46] $DataFile_cCelular2 = $DataFile[47] $DataFile_cEmail2 = $DataFile[48] $DataFile_cFormaTrat3 = $DataFile[49] $DataFile_cNome3 = $DataFile[50] $DataFile_cFuncao3 = $DataFile[51] $DataFile_cNascimento3 = $DataFile[52] $DataFile_cTelefone3 = $DataFile[53] $DataFile_cCelular3 = $DataFile[54] $DataFile_cEmail3 = $DataFile[55] $DataFile_Fumaca = $DataFile[56] $DataFile_Descarga = $DataFile[57] $DataFile_Paletizado = $DataFile[58] $DataFile_Interavia = $DataFile[59] $DataFile_PontoDescarga = $DataFile[60] $DataFile_TipoPagamento = $DataFile[61] $DataFile_Limite = $DataFile[62] $DataFile_GrupoSegmento = $DataFile[63] $DataFile_Segmento = $DataFile[64] EndFunc ;==>DadosDataFile Func ColetarDadosReceita() ;CNPJ $oTable = _IETableGetCollection($oIE, 4) $aTableData = _IETableWriteToArray($oTable) $RF_CNPJ = StringRegExpReplace($aTableData[0][0], "\D", "") ;DATA DE ABERTURA $RF_DataFundacao = StringTrimRight(StringRight($aTableData[2][0], 11), 1) ;RAZAO SOCIAL $oTable = _IETableGetCollection($oIE, 5) $aTableData = _IETableWriteToArray($oTable) $RF_RazaoSocial = StringTrimRight(StringTrimLeft($aTableData[0][0], 18), 1) ;NOME FANTASIA $oTable = _IETableGetCollection($oIE, 6) $aTableData = _IETableWriteToArray($oTable) $RF_NomeFantasia = StringTrimRight(StringTrimLeft($aTableData[0][0], 46), 1) ;ATIVIDADE PRIMÁRIA $oTable = _IETableGetCollection($oIE, 7) $aTableData = _IETableWriteToArray($oTable) $RF_AtividadePrimaria = StringLeft(StringTrimLeft($aTableData[0][0], 53), 10) ;ATIVIDADES SECUNDÁRIAS $oTable = _IETableGetCollection($oIE, 8) $aTableData = _IETableWriteToArray($oTable) $Temp_AtivSec = StringRegExpReplace($aTableData[0][0], "\D", "") $RF_QtdAtivSecs = StringLen($Temp_AtivSec) / 7 If $RF_QtdAtivSecs = "" Then $RF_AtividadeSecundaria = "Não informada" Else Dim $RF_AtividadeSecundaria[$RF_QtdAtivSecs] $i = 0 Do $RF_AtividadeSecundaria[$i] = StringLeft($Temp_AtivSec, 7) $MaskTemp = $RF_AtividadeSecundaria[$i] $RF_AtividadeSecundaria[$i] = StringLeft($RF_AtividadeSecundaria[$i], 2) & "." & StringLeft(StringTrimLeft($RF_AtividadeSecundaria[$i], 2), 2) & "-" & StringLeft(StringTrimLeft($RF_AtividadeSecundaria[$i], 4), 1) & "-" & StringRight($RF_AtividadeSecundaria[$i], 2) $Temp_AtivSec = StringTrimLeft($Temp_AtivSec, 7) $i = $i + 1 Until $i = $RF_QtdAtivSecs EndIf ;LOGRADOURO $oTable = _IETableGetCollection($oIE, 10) $aTableData = _IETableWriteToArray($oTable) $RF_Logradouro = StringTrimRight(StringTrimLeft($aTableData[0][0], 12), 1) ;NÚMERO $RF_Numero = StringTrimRight(StringTrimLeft($aTableData[2][0], 8), 1) ;COMPLEMENTO $RF_Complemento = StringTrimRight(StringTrimLeft($aTableData[4][0], 13), 1) ;CEP $oTable = _IETableGetCollection($oIE, 11) $aTableData = _IETableWriteToArray($oTable) $RF_CEP = StringTrimRight(StringTrimLeft($aTableData[0][0], 5), 1) ;BAIRRO $RF_Bairro = StringTrimRight(StringTrimLeft($aTableData[2][0], 17), 1) ;MUNICIPIO $RF_Municipio = StringTrimRight(StringTrimLeft($aTableData[4][0], 11), 1) ;UF $RF_UF = StringTrimRight(StringTrimLeft($aTableData[4][0], 11), 1) ;SITUAÇÃO $oTable = _IETableGetCollection($oIE, 12) $aTableData = _IETableWriteToArray($oTable) $RF_Situacao = StringTrimRight(StringTrimLeft($aTableData[0][0], 20), 1) EndFunc ;==>ColetarDadosReceita (Basically, it's a script that will collect data from a form and a website and fill SAP with these informations) If I need to use any of $RF_ variables outside the function, I can't. Will show an error of 'Variable used without being declared' Another solution is to use '#region ~ #endregion' instead of 'Func ~ EndFunc'. Again, I'm using function because it's easier to manipulate it later in the 'main function' Like: Func ActivateScript() StartFunction() If $RF_ExampleVariable = 1 Then Function_1 () Else Function_2 () EndIf EndFunc Link to comment Share on other sites More sharing options...
BrewManNH Posted August 24, 2010 Share Posted August 24, 2010 Just a quick note to the OP, if you want to run your script to test it, there's no need to compile it first. Just run it using AutoIt3.exe and put your scriptname as a command line parameter for it. Scripting languages are much simpler to test than "real" languages like C[++/#] or visual basic in that they don't need to be compiled to be run. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator 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