Jump to content
Sign in to follow this  
Broadcastic

Search all row?

Recommended Posts

Hello autoiters! How do I find whole row? Searching for row which looks like this  [-1,0,0]. Thanks in advance

Global $arrayVectors[26][3]=[[-1,-1,-1],[-1,-1,0],[-1,-1,1],[-1,0,-1],[-1,0,0],[-1,0,1],[-1,1,-1],[-1,1,0],[-1,1,1],[0,-1,-1],[0,-1,0],[0,-1,1],[0,0,-1],[0,0,1],[0,1,-1],[0,1,0],[0,1,1],[1,-1,-1],[1,-1,0],[1,-1,1],[1,0,-1],[1,0,0],[1,0,1],[1,1,-1],[1,1,0],[1,1,1]];


Dim $arr3[1][3]=[[-1,0,0]];
ConsoleWrite(_ArraySearch($arrayVectors,$arr3,0,0,0,0,1,-1,True));

Share this post


Link to post
Share on other sites

Store your values as strings: "-1-1-1", "-1-10" and so on. In this case, each string is unique and you can simple search for it. To search for first AND second AND third value in  your array, needs more effort than this way.


Best Regards BugFix  

Share this post


Link to post
Share on other sites

Without looping through the array multiple times, for each row/column, I would suggest converting it to a string and then search for the entire string, two examples below:

#Region Example 1 ;~ Search for single item
#include <Array.au3>
Global $g_aVectors[26][3]=[[-1,-1,-1],[-1,-1,0],[-1,-1,1],[-1,0,-1],[-1,0,0],[-1,0,1],[-1,1,-1],[-1,1,0],[-1,1,1],[0,-1,-1],[0,-1,0],[0,-1,1],[0,0,-1],[0,0,1],[0,1,-1],[0,1,0],[0,1,1],[1,-1,-1],[1,-1,0],[1,-1,1],[1,0,-1],[1,0,0],[1,0,1],[1,1,-1],[1,1,0],[1,1,1]];
Global $g_vSearchResults = _SearchExample1("-1|0|0")
    If @error Then
        MsgBox(4096, "Search Results", $g_vSearchResults)
    Else
        MsgBox(4096, "Search Results", "Search Result = " & $g_vSearchResults & @CRLF & _
        "$g_aVectors[" & $g_vSearchResults & "][0] = " & $g_aVectors[$g_vSearchResults][0] & @CRLF & _
        "$g_aVectors[" & $g_vSearchResults & "][1] =  " & $g_aVectors[$g_vSearchResults][1] & @CRLF & _
        "$g_aVectors[" & $g_vSearchResults & "][2] =  " & $g_aVectors[$g_vSearchResults][2] & @CRLF)
    EndIf

Func _SearchExample1($_sSearch)
    Local $sVectors = _ArrayToString($g_aVectors, "|", -1, -1, @CRLF)
    Local $aVectors = StringSplit($sVectors, @CRLF, 3)
    $iSearchVector = _ArraySearch($aVectors,$_sSearch)
            If @error Then Return SetError(1, 0, "No Search Results")
    Return SetError(0, 0, $iSearchVector)
EndFunc
#EndRegion Example 2

#Region Example 2 ;` Search for multiple items
Global $g_aSearchVectors[3][3]=[[-1,0,0], [1,0,-1], [-1,-1,31]]
Global $g_vSearchResults = _SearchExample2()
    If @error Then
        MsgBox(4096, "Search Results", $g_vSearchResults)
    Else
        For $i = 0 To UBound($g_vSearchResults) - 1
            MsgBox(4096, "Search Results", $g_vSearchResults[$i][1] = -1 ? "Search Item : " & $g_vSearchResults[$i][0] & @CRLF & "Search Item not found" : "Search Item : " & $g_vSearchResults[$i][0] & @CRLF & "Search Item Found Row : " & $g_vSearchResults[$i][1])
        Next
    EndIf

Func _SearchExample2()
    Local $sSearchVector, $aSearchResult[0][2], $iSearchVector
    If Not IsArray($g_aVectors) Then Exit
    If Not IsArray($g_aSearchVectors) Then Exit
    Local $sVectors = _ArrayToString($g_aVectors, "|", -1, -1, @CRLF)
    Local $aVectors = StringSplit($sVectors, @CRLF, 3)
    For $i = 0 To UBound($g_aSearchVectors) - 1
        $sSearchVector = _ArrayToString($g_aSearchVectors, "|", $i, $i, @CRLF)
        $iSearchVector = _ArraySearch($aVectors, $sSearchVector)
        If @error Then
            _ArrayAdd($aSearchResult, "[" & StringReplace($sSearchVector, "|", ",") & "]|-1")
        Else
            _ArrayAdd($aSearchResult, "[" & StringReplace($sSearchVector, "|", ",") & "]|" & $iSearchVector)
        EndIf
    Next
    Return SetError(0, 0, $aSearchResult)
EndFunc
#EndRegion Ezample 2

 

Share this post


Link to post
Share on other sites

@Broadcastic
Just converting the source array and the "find" array as strings, and looping through the source array "formatted", you can easily find the row/array elements you were looking for.

#include <Array.au3>
#include <StringConstants.au3>

Global $arrayVectors[8][3]=[[-1,-1,-1], _
                            [-1,-1, 0], _
                            [-1,-1, 1], _
                            [-1, 0,-1], _
                            [-1, 0, 0], _
                            [-1, 0, 1], _
                            [-1, 1,-1], _
                            [-1, 0, 1]], _
     $arrFind[1][3] = [[-1, 0, 1]], _
     $arrRowsFound

$arrRowsFound = _ArrayRowSearch($arrayVectors, $arrFind)
If IsArray($arrRowsFound) Then _ArrayDisplay($arrRowsFound)

; Returns the index(es) of the row(s) found in the source array containing the $arrFind elements in the row and in the columns
Func _ArrayRowSearch($arrArray, $arrFind)

    Local $strFind, _
          $arrResult[0]

    ; The source array became an array formatted
    $arrArray = StringSplit(_ArrayToString($arrArray), @CRLF, BitOR($STR_NOCOUNT, $STR_ENTIRESPLIT))

    ; The array to find become a string
    $strFind = _ArrayToString($arrFind)

    ; Loop through the array
    For $i = 0 To UBound($arrArray) - 1 Step 1
        If StringInStr($arrArray[$i], $strFind) > 0 Then _ArrayAdd($arrResult, $i)
    Next

    ; If has been found something
    If UBound($arrResult) > 0 Then
        Return $arrResult
    Else
        Return SetError(1, 0, 0)
    EndIf

EndFunc

:)


Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

You don't even need the array in the first place!

; Global $arrayVectors = [[-1,-1,-1],[-1,-1,0],[-1,-1,1],[-1,0,-1],[-1,0,0],[-1,0,1],[-1,1,-1],[-1,1,0],[-1,1,1],[0,-1,-1],[0,-1,0],[0,-1,1],[0,0,-1],[0,0,1],[0,1,-1],[0,1,0],[0,1,1],[1,-1,-1],[1,-1,0],[1,-1,1],[1,0,-1],[1,0,0],[1,0,1],[1,1,-1],[1,1,0],[1,1,1]];

; vectors can be regarded as values in base 3, with digits 0..2 shifted by -1
; since entries are in ascending order already, this is trivial

ConsoleWrite("index of entry [-1,0,1] is " & _Search(-1, 0, 1) & @LF)

Func _Search($x, $y, $z)
    Return (9 * ($x + 1) + 3 * ($y + 1) + ($z + 1))
EndFunc

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
54 minutes ago, jchd said:

Clever, jchd!  but [0,0,0] element was missing on purpose so you're off by 1! lol. Need to check if $x,$y,$z >0 and do -1. Great suggestion, thanks!

You don't even need the array in the first place!

; Global $arrayVectors = [[-1,-1,-1],[-1,-1,0],[-1,-1,1],[-1,0,-1],[-1,0,0],[-1,0,1],[-1,1,-1],[-1,1,0],[-1,1,1],[0,-1,-1],[0,-1,0],[0,-1,1],[0,0,-1],[0,0,1],[0,1,-1],[0,1,0],[0,1,1],[1,-1,-1],[1,-1,0],[1,-1,1],[1,0,-1],[1,0,0],[1,0,1],[1,1,-1],[1,1,0],[1,1,1]];

; vectors can be regarded as values in base 3, with digits 0..2 shifted by -1
; since entries are in ascending order already, this is trivial

ConsoleWrite("index of entry [-1,0,1] is " & _Search(-1, 0, 1) & @LF)

Func _Search($x, $y, $z)
    Return (9 * ($x + 1) + 3 * ($y + 1) + ($z + 1))
EndFunc

 

 

Share this post


Link to post
Share on other sites

My bad, I didn't look closely enough.

ConsoleWrite("index of entry [-1,-1,-1] is " & _Search(-1, -1, -1) & @LF)
ConsoleWrite("index of entry [0,0,-1] is " & _Search(0, 0, -1) & @LF)
ConsoleWrite("index of entry [0,0,0] is " & _Search(0, 0, 0) & @LF)     ; entry not found in original array
ConsoleWrite("index of entry [0,0,1] is " & _Search(0, 0, 1) & @LF)
ConsoleWrite("index of entry [1,1,1] is " & _Search(1, 1, 1) & @LF)

Func _Search($x, $y, $z)
    Local $value = 9 * ($x + 1) + 3 * ($y + 1) + $z + 1
    ; entry [0,0,0] = 13 is missing in array, so adjust upper values accordingly
    ; return -1 (invalid index) when $x = $y = $z = 0
    Return $value = 13 ? -1 : ($value > 13 ? $value - 1 : $value)
EndFunc

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Another example.

#include <Array.au3>

; Note: This example array has row indexes 4, 7, and 21 with the same row, [-1, 0, 0].
Global $arrayVectors[26][4] = [[-1, -1, -1], [-1, -1, 0], [-1, -1, 1], [-1, 0, -1], [-1, 0, 0], _ ; Index 4
        [-1, 0, 1], [-1, 1, -1], [-1, 0, 0], _ ;                                                    Index 7
        [-1, 1, 1], [0, -1, -1], [0, -1, 0], [0, -1, 1], [0, 0, -1], [0, 0, 1], [0, 1, -1], [0, 1, 0], _
        [0, 1, 1], [1, -1, -1], [1, -1, 0], [1, -1, 1], [1, 0, -1], [-1, 0, 0], _ ;                 Index 21
        [1, 0, 1], [1, 1, -1], [1, 1, 0], [1, 1, 1]] ;
Global $arr3[1][4] = [[-1, 0, 0, ""]] ; The row to be found
Global $sRowToFind = _ArrayToString($arr3, ",") ; Search row array, $arr3, to string

; --------------- Number of occurrences of the search row, $arr3, in the $arrayVectors array -----------
StringReplace(_ArrayToString($arrayVectors, ","), $sRowToFind, "")
Global $iOccurrences = @extended

If $iOccurrences Then
    Global $aIndex[$iOccurrences] ; Create an array to hold the indexes of the matching rows.
    For $i = 0 To $iOccurrences - 1
        ;Note: When $i = 0, _ArrayToString's parameter, $iStart_Row, = -1 (default value)
        $aIndex[$i] = UBound(StringRegExp($sRowToFind, "(" & _ArrayToString($arrayVectors, ",", ($i ? $aIndex[$i - 1] + 1 : 0), -1, ")|(") & ")", 3)) + ($i ? $aIndex[$i - 1] : -1)
    Next
    _ArrayDisplay($aIndex, "Matching Indexes")
Else
    MsgBox(0, "Result", "Row not found")
EndIf

#cs
; -------------- For understanding purposes only ---------------------
ConsoleWrite("$sRowToFind = " & $sRowToFind & @CRLF)
ConsoleWrite("Capture groups = (" & _ArrayToString($arrayVectors, ",", -1, -1, ")|(") & ")" & @CRLF)
$a = StringRegExp($sRowToFind, "(" & _ArrayToString($arrayVectors, ",", -1, -1, ")|(") & ")", 3) ;
ConsoleWrite("Note: $sRowToFind, """ & $sRowToFind & """ is " & UBound($a) & " capture groups from the left of the Capture groups string." & @CRLF)
ConsoleWrite("Index of first row found = " & UBound($a) - 1 & @CRLF)
_ArrayDisplay($a)
#ce

 

Share this post


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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...