Immensee Posted May 6, 2008 Share Posted May 6, 2008 (edited) Heya, haven't been here for a long time, because i always use autoit easiliy. But i am having trouble with a simple thing. The Global and Local scope doesn't work as i thought so. Normally, the GLOBAL scope enables a variable to be accessed in the whole script and a local only accessible inside it's position (script/function area access). Well i found out that this isn't the case in Autoit. Can someone explain me how it exactly works, because it isn't explained 100% in the autoit manual. It only tells: Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!) Global = Forces creation of the variable in the Global scope Local = Forces creation of the variable in the Local/Function scope ----------- I want to understand this scopes better to create my thingy for my mother and to be ready on mothers day . expandcollapse popupFunc DataSearch() Global $SEARCHITEM[$DATAMAX] MsgBox(0,"",$SEARCHITEM[1]) ;; --Renew List For $x=1 To $DATAMAX Step 1 If Not $SEARCHITEM[$x] = "" Then MsgBox(0,"", $SEARCHITEM[$x]) GUICtrlDelete($SEARCHITEM[$x]) Else ExitLoop EndIf Next ;; --Declaration Local $POST = GUICtrlRead($INPUTSEARCH[0]) Local $STRAAT = GUICtrlRead($INPUTSEARCH[1]) Local $NUMMER = GUICtrlRead($INPUTSEARCH[2]) Local $OCCURENCE[$DATAMAX] Local $OCCMAX Local $DATA_INDEX = FileIndex() Local $MATCHX Local $MATCHFOUND[$DATAMAX] If $POST="" Or $STRAAT="" Then MsgBox(032, "Zoeken gefaald", "Het zoeken is gefaald. Kijk of het postcode en het straat is ingevuld.") Else ;; --Rough Occurence Search For $x=1 To $DATA_INDEX Step 1 $DATA_STRING = FileReadLine($DATAFILE, $x) If $NUMMER = "" Then $RESULT = StringInStr($DATA_STRING, "[POSTCODE]"&$POST&"[/POSTCODE][STRAAT]"&$STRAAT&"[/STRAAT]") If Not $RESULT = 0 Then $OCCMAX+=1 $OCCURENCE[$OCCMAX] = $DATA_STRING EndIf Else $RESULT = StringInStr($DATA_STRING, "[POSTCODE]"&$POST&"[/POSTCODE][STRAAT]"&$STRAAT&"[/STRAAT]"&"[HUISNUMMER]"&$NUMMER&"[/HUISNUMMER]") If Not $RESULT = 0 Then $OCCMAX+=1 $OCCURENCE[$OCCMAX] = $DATA_STRING EndIf EndIf Next Local $SEARCHITEMX For $x=1 To $OCCMAX Step 1 $DATA_INPUT = $OCCURENCE[$x] If Not $DATA_INPUT = "" Then $POST = _StringBetween($DATA_INPUT, "[POSTCODE]", "[/POSTCODE]") $STREET = _StringBetween($DATA_INPUT, "[STRAAT]", "[/STRAAT]") $NUMBER = _StringBetween($DATA_INPUT, "[HUISNUMMER]", "[/HUISNUMMER]") $PHONE = _StringBetween($DATA_INPUT, "[TELEFOON]", "[/TELEFOON]") $MORE = _StringBetween($DATA_INPUT, "[COMMENT]", "[/COMMENT]") If Not @error = 1 Then $SEARCHITEMX+=1 $SEARCHITEM[$SEARCHITEMX] = GUICtrlCreateListViewItem($POST[0]&"|"&$STREET[0]&"|"&$NUMBER[0]&"|"&$PHONE[0]&"|"&$MORE[0], $LISTSEARCH) MsgBox(0,"", $SEARCHITEMX) EndIf EndIf Next EndIf EndFunc Everything works fine here, except that the array $SEARCHITEM cannot be read on the start of the script. I've tried to Globalize the $SEARCHITEM array, so that LISTITEM controls can be put into here and be deleted on the start of the script. well hope that people will quikly help me, my script is not done and mothers day is coming soon immense aka naruto Edited May 6, 2008 by Immense Link to comment Share on other sites More sharing options...
spudw2k Posted May 6, 2008 Share Posted May 6, 2008 More code would be useful. I'm pretty sure you shouldn't declare a Global var in a function that is re-run. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX BuilderMisc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose ArrayProjects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalcCool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF  Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 7, 2008 Share Posted May 7, 2008 Heya, haven't been here for a long time, because i always use autoit easiliy. But i am having trouble with a simple thing. The Global and Local scope doesn't work as i thought so. Normally, the GLOBAL scope enables a variable to be accessed in the whole script and a local only accessible inside it's position (script/function area access). Well i found out that this isn't the case in Autoit. Can someone explain me how it exactly works, because it isn't explained 100% in the autoit manual. It only tells: Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!) Global = Forces creation of the variable in the Global scope Local = Forces creation of the variable in the Local/Function scope Everything works fine here, except that the array $SEARCHITEM cannot be read on the start of the script. I've tried to Globalize the $SEARCHITEM array, so that LISTITEM controls can be put into here and be deleted on the start of the script. well hope that people will quikly help me, my script is not done and mothers day is coming soon immense aka naruto Dim is deprecated in AutoIt (per a syntax check message received when using the full verification options). That means it may go away some day soon. Explicit Global/Local is preferred. It's OK to change a global variable's value inside a function, but they should not be DECLARED inside a function. This will also generate warning from the syntax checker. Local should be used to declare variables inside a function whose values are only significant in that instance of the function. The Local declared inside a function can have the same name as an existing Global, but will not be the same variable and will not interfere with the Global variable's value. Global $MyVar _FuncOne() _FuncTwo() ConsoleWrite("$MyVar = " & $MyVar & @LF) Func _FuncOne() $MyVar = "Global set by _FuncOne" EndFunc Func _FuncTwo() Local $MyVar = "Local set by _FuncTwo" ConsoleWrite("$MyVar = " & $MyVar & @LF) EndFunc 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...
Immensee Posted May 7, 2008 Author Share Posted May 7, 2008 Aahh, that cleared alot for me, thanks PsaltyDS. So, if i create the array outside the function (so global) it will be able to be accessed from the function too, unless i write LOCAL into it, which will create an other array only available in the function (local scope)! So the following $array holds 4 listviewitems control identifiers, am i right ? (made a simple script) Global Const $CONSTANT_INFINITY = 5000 Global $array[$CONSTANT_INFINITY] Global $SOMEDATA[$CONSTANT_INFINITY] $SOMEDATA[1] = "bla | bla | bla" $SOMEDATA[2] = "bla | bla | bla" $SOMEDATA[3] = "bla | bla | bla" $SOMEDATA[4] = "bla | bla | bla" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func Test() Local $AMOUNT For $x=1 To $CONSTANT_INFINITY Step 1 If Not $SOMEDATA[$x] = "" Then $AMOUNT+=1 $array[$AMOUNT] = GUICTRLCREATELISTVIEWITEM(BLABLABLA) Else Exitloop EndIf Next EndFunc I have attached my full script in here. If you want to help me to solve this wicked puzzle please do Thanks for all help given ! immense aka narutoadressenboek___050508_1735__.au3 Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 7, 2008 Share Posted May 7, 2008 Aahh, that cleared alot for me, thanks PsaltyDS. So, if i create the array outside the function (so global) it will be able to be accessed from the function too, unless i write LOCAL into it, which will create an other array only available in the function (local scope)! So the following $array holds 4 listviewitems control identifiers, am i right ? (made a simple script) Global Const $CONSTANT_INFINITY = 5000 Global $array[$CONSTANT_INFINITY] Global $SOMEDATA[$CONSTANT_INFINITY] $SOMEDATA[1] = "bla | bla | bla" $SOMEDATA[2] = "bla | bla | bla" $SOMEDATA[3] = "bla | bla | bla" $SOMEDATA[4] = "bla | bla | bla" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func Test() Local $AMOUNT For $x=1 To $CONSTANT_INFINITY Step 1 If Not $SOMEDATA[$x] = "" Then $AMOUNT+=1 $array[$AMOUNT] = GUICTRLCREATELISTVIEWITEM(BLABLABLA) Else Exitloop EndIf Next EndFunc You've got the right idea, but this will fail when $x = $CONSTANT_INFINITY because arrays are indexed zero-based, so an array with 5000 elements ends at element 4999. Also, Step 1 is the default, so not needed here: For $x = 0 To $CONSTANT_INFINITY - 1 Another minor point is that you declare $AMOUNT with a default null value, which is not the same thing as 0, exactly. Since you intend to use it as an integer counter, consider "Local $AMOUNT = 0" instead. AutoIt automatically converts many variable types as required, and this probably works, but might not in every case. 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...
Immensee Posted May 7, 2008 Author Share Posted May 7, 2008 (edited) Yeah i know PsaltyDS, i know them both, but they won't result as a 'fatal' error. Also, i was making it very quick. Well i've tried to put the Global declaration outside the function zone, but it didnt work :S. Instead, it says that the subscriptions (amount of arrays available i.e [1000]) isn't enough, but that isn't true. So the program couldn't read the array if i put it in the global scope.... anyone can help ? My script is already in the attachments above. naruto aka immense Edited May 8, 2008 by Immense 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