kor Posted August 8, 2012 Share Posted August 8, 2012 why does the following generate a syntax error? Switch $sHail Case "StaffAdd" Global $aControls[9] = $aClean[$i][0], $aControls[11] = $aClean[$i][1] EndSwitch this works fine, but don't understand why each declaration needs to be on a separate line Switch $sHail Case "StaffAdd" $aControls[9] = $aClean[$i][0] $aControls[11] = $aClean[$i][1] EndSwitch This also works fine. It's the same concept just not arrays. Switch $sHail Case "StaffAdd" Local $x = 5, $y = 10 EndSwitch Link to comment Share on other sites More sharing options...
Spiff59 Posted August 9, 2012 Share Posted August 9, 2012 (edited) When using Global or Local (or Dim, but Dim is obsolete) to declare an array you can pre-assign values to the array but those values need to be defined using brackets that match the structure of the array, i.e.: Global $aArray[4] = [1,2,3,4] Local $aNames[2][3] = [["bob", "tom", "joe"],["sue", "ann", "deb"]] You can also only assign a portion of the array values. This would pre-assign values just for elements [0][0] and [1][0]: Local $aNames[2][3] = [["bob"],["sue"]] In declaration statements a variable such as "$aArray[9]" references (and defines) the entire array. If not preceeded by a Global or Local (or Dim) keyword any variable such as $aArray[9] is only referencing a single element (the 9th in this case) within an array. So, adding brackets to your first example would create an $aControls array and pre-assign a value to (just) the first element. Global $aControls[9] = [$aClean[$i][0]] Your second example successfully assigns a value to the 9th element of an already defined $aControls array. There is one exception in that an entire array can be created on-the-fly without using Global/Local/Dim if you assign another array to a variable. This basically creates a copy of $aArray1: Global $aArray1[4] = [1,2,3,4] $aArray2 = $aArray1 Edit: remove unwanted color tags from within code tags Edited August 9, 2012 by Spiff59 Link to comment Share on other sites More sharing options...
czardas Posted August 9, 2012 Share Posted August 9, 2012 You don't necessarilly need to write multiple lines. If it is possible to organize your array elements in a systematic way you can often loop through the elements. With multidimensional arrays use nested loops. One problem with your first example is that you are decalring the same array twice on the same line. First you declare it to have 9 elements, then you give it a value which isn't an array, then you give a value for element number 11. Basically it's all messed up. Realise that assigning a value to a variable is not the same as declaring it. Switch $sHail Case "StaffAdd" Global $aControls[12] = ["","","","","","","","","",$aClean[$i][0],"",$aClean[$i][1]] EndSwitch operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
kor Posted August 9, 2012 Author Share Posted August 9, 2012 I'm not defining the initial size of the array. I'm saying that Array element 9 = the value from another arrays element $i 0 Then I say that array element 11 = a value from another arrays element $i 1 The $aControls array is a 1 dimensional array that is previously declared as Global $aControls[40] Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 9, 2012 Moderators Share Posted August 9, 2012 (edited) kor,When you use the Global/Local keyword in front of an array it means you are declaring the array for the first time - you then need to place any values to be put into the array within [ ]. So thisGlobal $aControls[9] = [$aClean[$i][0]], $aControls[11] = [$aClean[$i][1]]destroys any previously declared arrays with that name (and the second destroys the first) and produces a single array like this:Element $aControls [0] $aClean[$i][1] [1] - ... [10] -Which I am sure is not what you want. If you only want to assign values to the particular elements of the previously declared $aControls array, then just lose the initial Global:$aControls[9] = $aClean[$i][0] $aControls[11] = $aClean[$i][1]Then you will get the following array:Element $aControls [0] Existing value [1] Existing value ... [9] $aClean[$i][0] [10] Existing value [11] $aClean[$i][1] ... [39] Existing valueAll clear? M23 Edited August 9, 2012 by Melba23 Typo Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted August 9, 2012 Share Posted August 9, 2012 (edited) I'm not defining the initial size of the array. I'm saying that Array element 9 = the value from another arrays element $i 0 Then I say that array element 11 = a value from another arrays element $i 1 The $aControls array is a 1 dimensional array that is previously declared as Global $aControls[40] Ah but that's what you are trying to say. When you use syntax like Global $array[9] you declare that the array has 9 elements. Like Melba said, any subsequent declarations destroy the previous array and all it's contents. Also, you will have to assign a value to each element on a separate line unless you organize the elements in a way that enables you to assign values using a loop. The code I posted declared and assigned values to two elements of a 12 element array in one hit, and I used 12 elements because that is the minimum number required to rewrite your code accurately. However you don't need to declare the array twice, as pointed out by Melba. Edited August 9, 2012 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
kor Posted August 9, 2012 Author Share Posted August 9, 2012 $i = 1 Global $aClean[40], $aControls[40], $sHail _SetBulkInputs($aClean, $i) Func _SetBulkInputs(ByRef $aClean, $i) Switch $sHail Case "StaffAdd" $aControls[9] = $aClean[$i][0], $aControls[11] = $aClean[$i][1] EndSwitch EndFunc C:UsersuserDesktopodinddfdf.au3(9,34) : ERROR: syntax error $aControls[9] = $aClean[$i][0], ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ C:UsersuserDesktopodinddfdf.au3 - 1 error(s), 0 warning(s) Link to comment Share on other sites More sharing options...
czardas Posted August 9, 2012 Share Posted August 9, 2012 (edited) Hmm. you declared Global $aClean[40] as a one dimensional array. Next you reference it as if it was a two dimensional array $aClean[$i][0]. Also you need to assign values for $aControls[9] and $aControls[11] on separate lines. $i = 1 Global $aClean[40][2], $aControls[40], $sHail _SetBulkInputs($aClean, $i) Func _SetBulkInputs(ByRef $aClean, $i) Switch $sHail Case "StaffAdd" $aControls[9] = $aClean[$i][0] $aControls[11] = $aClean[$i][1] EndSwitch EndFunc But this code won't do anything because there's not enough of it. Edited August 9, 2012 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
kor Posted August 9, 2012 Author Share Posted August 9, 2012 Also you need to assign values for $aControls[9] and $aControls[11] on separate lines. thats exactly my initial question in my first post. Why do I have to assign the values on separate lines when you can assign values to other variables on the same line as referenced in my samples from my first post, but you can't do it when the variables are arrays. Link to comment Share on other sites More sharing options...
BrewManNH Posted August 9, 2012 Share Posted August 9, 2012 Because when you use Global/Local then you're declaring a variable/array, you're not using it to assign a value to that variable, although you can do both at the same time. $Array1 doesn't exist, "Global $Array1[9]" = Create this array called $Array1 and give it enough space for 9 entries. Now if you want to put something into $Array1[8], you need to do it either on a separate line, or in the declaration statement as shown in post 3. $Array1 exists, "Global $Array1[9]" = Destroy previous array, and create a new one with space for 9 entries. 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...
czardas Posted August 9, 2012 Share Posted August 9, 2012 Thanks BrewmanNH. kor have you read the wiki article about arrays? http://www.autoitscript.com/wiki/Arrays operator64 ArrayWorkshop 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