PapaOz Posted January 20, 2023 Share Posted January 20, 2023 I can do Global array[2] = ["one", "two"] or I can do Global array[2] array[0] = "one" array[1] = "two" But I can not do Global array[2] array = ["one", "two"] Why does this not work? Is there any way (i.e. setting) that will make it work? Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted January 20, 2023 Share Posted January 20, 2023 (edited) Hi @PapaOz, yes this is the case. Syntax error for the last variant: #include-once #include <Array.au3> Global $aTest1[2] = ["one", "two"] _ArrayDisplay($aTest1) ; works Global $aTest2[2] $aTest2[0] = "one" $aTest2[1] = "two" _ArrayDisplay($aTest2) ; works Global $aTest3[] = _ [ _ 'one', _ 'two' _ ] _ArrayDisplay($aTest3) ; works ; syntax error: ; Global $aTest4[2] = ["one", "two"] ; $aTest4 = ["one", "two"] ; _ArrayDisplay($aTest4) But why do you need this exactly this syntax? It's not AutoIt valid. The other three variants are fine so I don't see the problem to be honest!? Best regards Sven Edited January 20, 2023 by SOLVE-SMART Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
abberration Posted January 20, 2023 Share Posted January 20, 2023 Perhaps the easiest way to create an array: #include <Array.au3> $array = StringSplit("one|two|three|birds in a tree", "|", $STR_NOCOUNT) _ArrayDisplay($array) SOLVE-SMART and taurus905 2 Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
PapaOz Posted January 21, 2023 Author Share Posted January 21, 2023 Thank you. @SOLVE-SMART for responding to my post. I don't "need" the exact syntax I asked about. I agree that the multi-line solutions work. I was just curious why a syntax that works in a declaration doesn't also work in an assignment. I have to declare my array as Global before I can use it, but at the time I declare it I do not know how many elements it will need to hold so I give it [0]. Later, within a function, I ReDim the array and populate it with several variables. If I do that with individual assignments to each element it results in a lot of lines in the script. I would prefer to be able to make the assignments in a single statement. It's a personal style issue. Thank you, @abolutionist for reminding me that a function can return an array. I think that will be satisfactory and I will mark it as a solution. I am at best an "advanced beginner" with AutoIt so I am grateful for these forums. They usually have answers to my questions that search engines can find. I couldn't find anything like the responses I got to this thread with Google, so I posted here. Thank you both for your help. Link to comment Share on other sites More sharing options...
PapaOz Posted January 21, 2023 Author Share Posted January 21, 2023 Sigh.... Well, I botched it and marked my own post as a solution, but I'm sure any readers will figure i out. Link to comment Share on other sites More sharing options...
TimRude Posted January 21, 2023 Share Posted January 21, 2023 (edited) 6 hours ago, PapaOz said: I have to declare my array as Global before I can use it, but at the time I declare it I do not know how many elements it will need to hold so I give it [0]. Later, within a function, I ReDim the array and populate it with several variables. If I do that with individual assignments to each element it results in a lot of lines in the script. I would prefer to be able to make the assignments in a single statement. It's a personal style issue. @PapaOz Are you aware that even though you declared your array as Global at the beginning of the script, you can still re-declare it as Global again (even if inside a function) when you want to populate it later if you intend to completely replace the array contents with your new contents? You only need to ReDim the array if you want to keep the existing contents and add to it. Using the Global keyword lets you do all of your assignments in a single statement as you said you wanted to. It even resizes the array if needed since it's really just replacing the Global array you initially declared. #include <Array.au3> Global $aArray[2] ; declare a global array with 2 elements SomeFunction() _ArrayDisplay($aArray) Func SomeFunction() Global $aArray = ["One", "Two", "Three", "Four"] ; populate the global array with 4 elements EndFunc Edited January 21, 2023 by TimRude Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted January 21, 2023 Share Posted January 21, 2023 Hi @TimRude, I agree with you that this is a theoretical valid approach, but it's also "code smell" because the declaration of a global scoped variable (array) within a function isn't okay (is wrong). You could do it in this way, yes, but since there are four other ways to handle this, please do not suggest it. Anyways, thanks for different mind approach 👍 . Best regards Sven Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
TimRude Posted January 22, 2023 Share Posted January 22, 2023 @SOLVE-SMART Respectfully, I don't see a problem with redeclaring a global array inside a function, especially if the array was originally declared as Global already in the main code section. Global is global. The whole point of a global variable or array is to allow it to be read/set from anywhere in the code. In this case all you're doing by reusing the Global keyword is conforming to the syntax requirements in order to assign values to an already global array. I know some programming languages may not allow declaring a global variable inside a function and that's their prerogative. But if AutoIt is flexible enough to handle it, so am I. SOLVE-SMART and pixelsearch 2 Link to comment Share on other sites More sharing options...
RTFC Posted January 22, 2023 Share Posted January 22, 2023 (edited) Don't ReDim, don't redeclare, and don't return an entire array (needlessly moving data back and forth); just parse ByRef: #include <Array.au3> Local $aArrayTarget[5] = ["0", "1", "2", "3", "4"] _AddLocallyDefinedArray($aArrayTarget) _ArrayDisplay($aArrayTarget, "Target and Source concatenated") Func _AddLocallyDefinedArray(ByRef $aTarget) If Not (IsArray($aTarget) and UBound($aTarget,0)=1) Then Return Local $aArraySource[5] = ["5", "6", "7", "8", "9"] _ArrayConcatenate($aTarget, $aArraySource) EndFunc This way it'll work regardless of whether the target is global. Edited January 22, 2023 by RTFC sloppy copy SOLVE-SMART 1 My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted January 22, 2023 Share Posted January 22, 2023 9 hours ago, TimRude said: @SOLVE-SMART Respectfully, I don't see a problem with redeclaring a global array inside a function, especially if the array was originally declared as Global already in the main code section. Global is global. The whole point of a global variable or array is to allow it to be read/set from anywhere in the code. In this case all you're doing by reusing the Global keyword is conforming to the syntax requirements in order to assign values to an already global array. I know some programming languages may not allow declaring a global variable inside a function and that's their prerogative. But if AutoIt is flexible enough to handle it, so am I. Fair enough @TimRude. Thanks for your statement 👍 . I didn't want to forbid you to do this, but I also would not suggest this way/approach. I guess and hope @PapaOz already have a feeling about the "why" and how to handle such situations in AutoIt through the posts here 😀 . Best regards Sven Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
mistersquirrle Posted January 30, 2023 Share Posted January 30, 2023 On 1/21/2023 at 8:42 AM, PapaOz said: I don't "need" the exact syntax I asked about. I agree that the multi-line solutions work. I was just curious why a syntax that works in a declaration doesn't also work in an assignment. Basically, you can use that syntax when you create the array because you're defining its size/scope that that point. The ['value','value2'] syntax is a sequential assignment of values, but it (to me) doesn't make much sense to use that syntax after the array is created/modified. Say you made $aArray[10], and you wanted to update indexes 6,7,8, what would that look like? [Default,Default,Default,Default,Default,Default,'updated6','updated7','updated8']? That seems like to me it's a good reason why it's not supported after declaration. It's probably only supported during declaration because of a feature request. Here's an example of a post from 2005 where that syntax wasn't yet supported. We ought not to misbehave, but we should look as though we could. 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