zone97 Posted February 9, 2017 Share Posted February 9, 2017 (edited) I have read where people have helped with this in the past, but none match the situation I have. I have a 4 part repeating array. say $aRRAY = ["1","2","3","4","a1","a2","a3","a4", "b1","b2","b3","b4"] and I want to separate it into 2D array every 4 elements, part of the issue is you have to count the number of elements as its constantly going to change, but will always be in sets of 4. so it would end up like 1 2 3 4 a1 a2 a3 a4 b1 b2 b3 b4 Like that? Edited February 9, 2017 by zone97 Spoiler WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ] Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 9, 2017 Moderators Share Posted February 9, 2017 zone97, Simple maths gives you the solution: #include <Array.au3> Global $aArray = ["1","2","3","4","a1","a2","a3","a4", "b1","b2","b3","b4"] $iRows = Int(UBound($aArray) / 4) Global $aNewArray[$iRows][4] $iIndex = 0 For $i = 0 To $iRows - 1 For $j = 0 To 3 $aNewArray[$i][$j] = $aArray[$iIndex] $iIndex += 1 Next Next _ArrayDisplay($aNewArray, "", Default, 8) M23 zone97 1 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...
zone97 Posted February 9, 2017 Author Share Posted February 9, 2017 (edited) Perfect as always Melba.. God, I have got to get my head around this.. Edited February 9, 2017 by zone97 Spoiler WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ] Link to comment Share on other sites More sharing options...
jguinch Posted February 9, 2017 Share Posted February 9, 2017 I remember a made a function for this need : #Include <Array.au3> ; just for _ArrayDisplay Local $aRRAY = ["1","2","3","4","a1","a2","a3","a4", "b1","b2","b3","b4"] Local $a2DArray = _Array1DTo2D($aRRAY, 4) _ArrayDisplay($a2DArray) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
zone97 Posted February 9, 2017 Author Share Posted February 9, 2017 Awesome Also. Spoiler WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ] Link to comment Share on other sites More sharing options...
Malkey Posted February 10, 2017 Share Posted February 10, 2017 To help with your head, here is a modified Melba23 example basically the same but without an incrementing index. #include <Array.au3> Global $aArray = ["1", "2", "3", "4", "a1", "a2", "a3", "a4", "b1", "b2", "b3", "b4"] Global $iNumOfCols = 4 Global $iRows = Int(UBound($aArray) / $iNumOfCols) Global $aNewArray[$iRows][$iNumOfCols] For $i = 0 To $iRows - 1 For $j = 0 To $iNumOfCols - 1 $aNewArray[$i][$j] = $aArray[($i * $iNumOfCols) + $j] Next Next ConsoleWrite('A 1D array treated as a 4 column 2D array whose row and column indexes are [1][1] = "' & _ _Array1DArrayTo2DArray($aArray, 4, 1, 1) & '"' & @CRLF) ; Returns:- "a2" _ArrayDisplay($aNewArray) ; Returns an element of a 1D array with the row and column indexes of a 2D array. (Without error catching routines for rubbish in.) Func _Array1DArrayTo2DArray($1DArray, $iNumOfCols2D, $iRow_Ndx, $iCol_Ndx) Return $1DArray[($iNumOfCols2D * $iRow_Ndx) + $iCol_Ndx] EndFunc ;==>_Array1DArrayTo2DArray A 2d array could be viewed as a divided up version of a 1D array. (See the mental picture) The 1D array is divided up by the number of columns required. So that each full row of the 2D array contains the number of columns required. Link to comment Share on other sites More sharing options...
iamtheky Posted December 3, 2019 Share Posted December 3, 2019 Quote i not saw it thankz so much i like it , why this udf is not introduction on core of autoit ??? It's not really a common enough use case to be legitimately entertained, but this question in @jguinch's thread made me explore if the result could be achieved given existing UDFs. With no concern for being the optimal solution, just ones using UDFs and the fewest of them. down to 3 right now: #Include <Array.au3> Local $sString = "<select>" & @CRLF & _ " <option value='volvo'>Volvo</option>" & @CRLF & _ " <option value='saab'>Saab</option>" & @CRLF & _ " <option value='mercedes'>Mercedes</option>" & @CRLF & _ " <option value='audi'>Audi</option>" & @CRLF & _ " <option value='porsche'>Porsche</option>" & @CRLF & _ "</select>" Local $aValues = StringRegExp($sString, "value='([^']+)'>([^<]+)", 3) _ArrayDisplay($aValues, "1D Array") ;~ $count = 2 $count = 5 local $aOut[0][$count] For $i = 0 to ubound($aValues) - 1 step $count $aX = _ArrayExtract($aValues , $i , $i + $count - 1) _ArrayTranspose($aX) _ArrayAdd($aOut , $aX) Next _ArrayDisplay($aOut) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) 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