zuladabef Posted August 27, 2019 Posted August 27, 2019 I have an array of data and then I want to search/filter that data down based upon a criteria. What is the best practice for this? I was thinking I have to use _ArrayFindAll and put that into another array, but I ran into an issue with looping through that data. _ArrayFindAll returns indices, but how do I loop through all indices and store the data separately? Or, is there another/better way to do this? Local $aAllEntriesByStaffName = _ArrayFindAll($aStaffAbsenceData, $sName) Local $aByName[$aAllEntriesByStaffName[0]] For $xCC = 1 To UBound($aAllEntriesByStaffName) - 1 $aByName[$xCC] = Next
Subz Posted August 27, 2019 Posted August 27, 2019 (edited) Try: Local $aAllEntriesByStaffName = _ArrayFindAll($aStaffAbsenceData, $sName) Local $aByName[$aAllEntriesByStaffName[0] + 1] For $xCC = 1 To $aAllEntriesByStaffName[0] $aByName[$xCC] = $aStaffAbsenceData[$aAllEntriesByStaffName[$xCC]] Next Here is a working example: #include <Array.au3> Local $sSearchName = "bb" Local $aSearchData[] = [10, "aa", "bb", "cc", "dd", "aa", "bb", "cc", "dd", "aa", "bb"] Local $aFindAll = _ArrayFindAll($aSearchData, $sSearchName) If @error Then Exit Local $aSearchResults[$aFindAll[0] + 1] For $i = 1 To $aFindAll[0] $aSearchResults[$i] = $aSearchData[$aFindAll[$i]] Next $aSearchResults[0] = UBound($aSearchResults) - 1 _ArrayDisplay($aSearchResults) Edited August 27, 2019 by Subz Included working example
zuladabef Posted September 4, 2019 Author Posted September 4, 2019 So, I can't seem to get it to work on my side. Does it matter if $aSearchData is a 2D array? I keep getting this: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: $aSearchResults[$i] = $aStaffAbsenceData[$aFindAll[$i]] $aSearchResults[$i] = ^ ERROR On 8/27/2019 at 2:31 PM, Subz said: Try: Local $aAllEntriesByStaffName = _ArrayFindAll($aStaffAbsenceData, $sName) Local $aByName[$aAllEntriesByStaffName[0] + 1] For $xCC = 1 To $aAllEntriesByStaffName[0] $aByName[$xCC] = $aStaffAbsenceData[$aAllEntriesByStaffName[$xCC]] Next Here is a working example: #include <Array.au3> Local $sSearchName = "bb" Local $aSearchData[] = [10, "aa", "bb", "cc", "dd", "aa", "bb", "cc", "dd", "aa", "bb"] Local $aFindAll = _ArrayFindAll($aSearchData, $sSearchName) If @error Then Exit Local $aSearchResults[$aFindAll[0] + 1] For $i = 1 To $aFindAll[0] $aSearchResults[$i] = $aSearchData[$aFindAll[$i]] Next $aSearchResults[0] = UBound($aSearchResults) - 1 _ArrayDisplay($aSearchResults)
Nine Posted September 4, 2019 Posted September 4, 2019 17 minutes ago, zuladabef said: Does it matter if $aSearchData is a 2D array? Yes it matters a lot. Read carefully help file, you will need to do a loop search for each column (there is an option for row also) of your 2D array. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Subz Posted September 4, 2019 Posted September 4, 2019 If you only wanting to capture a single columns data for example the first column (the first column starts at index 0), just change your loop to: For $i = 1 To $aFindAll[0] $aSearchResults[$i] = $aSearchData[$aFindAll[$i]][0] Next You may also want to change the _ArrayFindAll to search a specific column, just see the help file for more info.
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