Sign in to follow this  
Followers 0
kor

array var = array var multiple declarations on same line

11 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Spiff59

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 this

Global $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 value

All clear? :)

M23

Edited 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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

$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)

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 Gude
How 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

Share this post


Link to post
Share on other sites

Thanks BrewmanNH.

kor have you read the wiki article about arrays? http://www.autoitscript.com/wiki/Arrays

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0