Jump to content

Search array for Multiple values in one call


Recommended Posts

Hello,

Is there a way to search any array for multiple items in one call , and have it return the position number  of the items in array separated by a semicolon.

For an example an 2d array search column 0 for values 5,6,7,test,edit,merge , and when it finds a match for any of the search items return a string with the position in the array

followed by a semicolon so that result can be used to delete the Items from the array, The array would be under 100 records with 15 columns?

Link to comment
Share on other sites

Link to comment
Share on other sites

Look at _ArraySearch with a compare value of 3?  The comma delimited list needs to become a pipe delimited list to be passed as a regex saying 'this or this or this or this', but it should do the job.

edit:  I suppose the loop of those is pretty much arrayfindall, so yeah build yer own one of those :)

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Use this :

#include <Array.au3>

Local $aSearch = [5,6,7,"test","edit","merge"]

Local $aArray[5][5] = [[0, 1, 2, 1, 1222], _
        [4, 5, 5, 4, 5], _
        [4, 1, 3, 1, "test"], _
        [0, 3, 2, 1, "abcdef"], _
        [1, 5, 5, 4, "merge"]]
_ArrayDisplay($aArray, "2D array")

Local $sSearch = "^" & _ArrayToString ($aSearch) & "$"
;MsgBox ($MB_SYSTEMMODAL,"",$sSearch)

Local $aResult = _ArrayFindAll($aArray, $sSearch, Default, Default, Default, 3, 4)
_ArrayDisplay($aResult, "Found in Column 4")

 

Link to comment
Share on other sites

And another example.

#include <Array.au3>
; https://www.autoitscript.com/forum/topic/200817-search-array-for-multiple-values-in-one-call/

Global $sSearch
Local $aFind = [5, "[2-7]", 7, "test", "edit", "merge"]

Local $aArray[6][5] = [[0, 1, 2, 1, 1223], _
        [4, 5, 5, 4, 5], _
        [4, 1, 3, 1, "testify"], _
        [0, 3, 2, 1, "abcdef"], _
        [4, 2, 3, 1, "This is a test."], _
        [1, 5, 5, 4, "merge"]]
_ArrayDisplay($aArray, "2D array")

; Find matching items.
$aMatchIndices = _ArrayFindItems($aArray, $aFind, 1) ; <----------- This is the one call to search an array for multiple values.
_ArrayDisplay($aMatchIndices, "Inndices (0-based) Matches", "", 0, Default, "Row|Column")

; Remove matching items
For $i = 0 To UBound($aMatchIndices, 1) - 1
    $aArray[$aMatchIndices[$i][0]][$aMatchIndices[$i][1]] = "-" ; Or StringRegExpReplace($aArray[$aMatchIndices[$i][0]][$aMatchIndices[$i][1]], "(" & $sSearch & ")", "") ;
Next
_ArrayDisplay($aArray, "Matched Items Deleted from original 2D array")


; $bSearchLiteral = 0 - Will match "test" in "testify", and, "[2-7]" will match all number characters 2 to 7 in each item of the array, $aArray.
;                 = 1 - Will not match "test" in "testify" (because of "\b"s in RE Pattern), and, all search characters in "$aSearch" array are matched literally ("\Q...\E").
; Returns a 0-based array with row index of a matching item of $aArray in first column (Col 0), and, the
;                           column index of a matching item of $aArray in second column (col 1).
;
Func _ArrayFindItems($aArray, $aSearch, $bSearchLiteral = 1)
    $sSearch = $bSearchLiteral ? "^\Q" & _ArrayToString($aSearch, "\E\b|\b\Q") & "\E$" : "^" & _ArrayToString($aSearch) & "$"
    ;MsgBox(0, "RegExp Pattern", $sSearch) ; $MB_OK = 0
    Local $iRow = 0, $aRet[100][2]
    For $i = 0 To UBound($aArray, 1) - 1
        For $j = 0 To UBound($aArray, 2) - 1
            If StringRegExp($aArray[$i][$j], $sSearch) Then
                $aRet[$iRow][0] = $i ; Row index
                $aRet[$iRow][1] = $j ; Column index
                $iRow += 1
            EndIf
        Next
    Next
    ReDim $aRet[$iRow][2]
    Return $aRet
EndFunc   ;==>_ArrayFindItems

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...