Jump to content

_ArrayFindAll() filtering data from an array


Recommended Posts

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

 

Link to post
Share on other sites

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 by Subz
Included working example
Link to post
Share on other sites

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)

 

 

Link to post
Share on other sites
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.

Link to post
Share on other sites

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.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...