litlmike Posted July 15, 2008 Share Posted July 15, 2008 Is there a "convention" for adding a column to a 1-D array? This is one of those answers that I should know, but I have been avoiding this one for awhile and just kept coming up with a work around for the particular situations. It is about time I address it. What is the proper way to accomplish this? So if I had these as inputs: Local $aArray1[3] = [1, 2, 3] Local $aArray2[3] = ["A", "B", "C"] And wanted this as the output: Local $aArray1[3][2] = [[1,"A"],[2,"B"],[3,"C"]] My guess is something like: Local $aArray1[3] = [1, 2, 3] Local $aArray2[3] = ["A", "B", "C"] For #of Arrays ReDim $aArray1[3][$i] = [ [ aArray1[$i], $aArray2[$i] ] Next _ArrayPermute()_ArrayUnique()Excel.au3 UDF Link to comment Share on other sites More sharing options...
PsaltyDS Posted July 15, 2008 Share Posted July 15, 2008 Is there a "convention" for adding a column to a 1-D array? This is one of those answers that I should know, but I have been avoiding this one for awhile and just kept coming up with a work around for the particular situations. It is about time I address it. What is the proper way to accomplish this? So if I had these as inputs: Local $aArray1[3] = [1, 2, 3] Local $aArray2[3] = ["A", "B", "C"] And wanted this as the output: Local $aArray1[3][2] = [[1,"A"],[2,"B"],[3,"C"]] My guess is something like: Local $aArray1[3] = [1, 2, 3] Local $aArray2[3] = ["A", "B", "C"] For #of Arrays ReDim $aArray1[3][$i] = [ [ aArray1[$i], $aArray2[$i] ] Next Couple of problems: 1. You can't change the number of dimensions/indexes without losing the data. 2. You can't preset data with ReDim the way you can with Dim/Local/Global. So in this case, you need to declare a new array, then use a loop to add all the data: #include <Array.au3> Global $aArray1[3] = [1, 2, 3] Global $aArray2[3] = ["A", "B", "C"] Global $aArray3[UBound($aArray1)][2] For $row = 0 To UBound($aArray1) - 1 $aArray3[$row][0] = $aArray1[$row] $aArray3[$row][1] = $aArray2[$row] Next _ArrayDisplay($aArray3, "Result") muttley 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...
litlmike Posted July 15, 2008 Author Share Posted July 15, 2008 Couple of problems: 1. You can't change the number of dimensions/indexes without losing the data. 2. You can't preset data with ReDim the way you can with Dim/Local/Global. So in this case, you need to declare a new array, then use a loop to add all the data: #include <Array.au3> Global $aArray1[3] = [1, 2, 3] Global $aArray2[3] = ["A", "B", "C"] Global $aArray3[UBound($aArray1)][2] For $row = 0 To UBound($aArray1) - 1 $aArray3[$row][0] = $aArray1[$row] $aArray3[$row][1] = $aArray2[$row] Next _ArrayDisplay($aArray3, "Result") muttley Thanks you kindly for clearing up the ReDim limitations and showing the convention here. _ArrayPermute()_ArrayUnique()Excel.au3 UDF Link to comment Share on other sites More sharing options...
litlmike Posted July 15, 2008 Author Share Posted July 15, 2008 One other question: If I am going to use _ArrayAdd to fill an array, what is the best practice to declare the 'blank' array and fill it? I have been using: Local $aArray[1]=[""] For $i=0 To 5 _ArrayAdd($aArray, $i) Next Then ending with either: _ArrayDelete($aArray, 0) or $aArray[0]=Ubound($aArray) _ArrayPermute()_ArrayUnique()Excel.au3 UDF Link to comment Share on other sites More sharing options...
PsaltyDS Posted July 15, 2008 Share Posted July 15, 2008 One other question: If I am going to use _ArrayAdd to fill an array, what is the best practice to declare the 'blank' array and fill it? I have been using: Local $aArray[1]=[""] For $i=0 To 5 _ArrayAdd($aArray, $i) Next Then ending with either: _ArrayDelete($aArray, 0) or $aArray[0]=Ubound($aArray) You need to decide if you know what the first values should be when you declare it or must fill them in later, and if [0] will be used as a count or as a data element: Local $aArray[6] = [0,1,2,3,4,5]; [0] = first data element ; - or - Local $aArray[6] = [5,1,2,3,4,5]; [0] = count ; - or - Local $aArray[1]; [0] = first data element ; ... filling in later: Local $n = 5 ReDim $aArray[$n + 1] For $i=0 To $n $aArray[$i] = $i Next ; - or - Local $aArray[1]=[0]; [0] = count For $i=1 To 5 _ArrayAdd($aArray, $i) Next $aArray[0] = Ubound($aArray) - 1 ; ...etc., too many variations to list muttley 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...
litlmike Posted July 15, 2008 Author Share Posted July 15, 2008 You need to decide if you know what the first values should be when you declare it or must fill them in later, and if [0] will be used as a count or as a data element: Local $aArray[6] = [0,1,2,3,4,5]; [0] = first data element ; - or - Local $aArray[6] = [5,1,2,3,4,5]; [0] = count ; - or - Local $aArray[1]; [0] = first data element ; ... filling in later: Local $n = 5 ReDim $aArray[$n + 1] For $i=0 To $n $aArray[$i] = $i Next ; - or - Local $aArray[1]=[0]; [0] = count For $i=1 To 5 _ArrayAdd($aArray, $i) Next $aArray[0] = Ubound($aArray) - 1 ; ...etc., too many variations to list muttley Ahhh, thanks. I like this, it is always good to know what conventions I should be using. So then, Local $aArray[1]=[0]; ending with $aArray[0] = Ubound($aArray) - 1, looks like the most reasonable method for most situations. When you are in the middle of a script, how do you personally decide "if [0] ... should be used as a count or as a data element"? _ArrayPermute()_ArrayUnique()Excel.au3 UDF Link to comment Share on other sites More sharing options...
JFee Posted July 15, 2008 Share Posted July 15, 2008 Well it depends on your goal. If you are going to return the array from a function, I usually use [0] as a count. Other times... I still start at [1] for my data muttley Regards,Josh Link to comment Share on other sites More sharing options...
PsaltyDS Posted July 15, 2008 Share Posted July 15, 2008 When you are in the middle of a script, how do you personally decide "if [0] ... should be used as a count or as a data element"? That's a design decision you make to fit the situation at the time you are coding the script. If you need to use references to the count often, "$aArray[0]" might be cleaner than "Ubound($aArray) - 1". If your array will be passed to other functions as a parameter, then you go with whatever those functions expect. Just go with what makes sense at the time, and note your decision for future reference by a comment where you declare the array! muttley 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...
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