Jump to content

_ArraySearch , need EXACT match


Go to solution Solved by mikell,

Recommended Posts

Local $aValues[2][2] = [["JobTitle1","Health Assistant, Health Aide"],["JobTitle2","Health Advisor"]]
Local $sSearchTerm = "Health"
Local $bMatchFound = False

For $i = 0 To UBound($aValues, 1) -1
    For $j = 0 To UBound($aValues, 2) -1
        If $aValues[$i][$j] == $sSearchTerm Then ; We found the exact term so do something.
            ConsoleWrite("Match found at $aValues[" & $i & "][" & $j & "]" & @LF) ; Show array syntax in SciTE console
            $bMatchFound = True
            ExitLoop 2 ; Or not, if you wish to continue searching
        EndIf
    Next
Next

MsgBox(0, "Exact Match Found", $bMatchFound)

For case insensitive search replace == with a single equals sign.

Edited by czardas
Link to post
Share on other sites
  • Developers

Neither the exact term "Health Aide" does...

Which makes sense as that value of the array cell is:

 $aValues[$i][$j] = Health Assistant, Health Aide

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

That's it Jos  :)

I tried the regex in _ArraySearch with flag 3 in the loop (redundant and heavy way though), but it fails on $i = 0 because _ArraySearch thinks that this 0 value is the default one meaning "search whole array"

For $i = 0 To UBound($aValues) - 1
    ConsoleWrite($aValues[$i][0] & " > ")
   ; ConsoleWrite(StringRegExp($aValues[$i][1], '(?i)(^|,)\h*' & $sSearch & '(?!\h*\w+)') & @CR)
    ConsoleWrite(_ArraySearch($aValues, '(?i)(^|,)\h*' & $sSearch & '(?!\h*\w+)', $i, $i, 0, 3, 1, 1) & @CR)
Next

Anyway why not (as JohnOne suggested) use in a simple loop the proper working regular expression ?

Edited by mikell
Link to post
Share on other sites

mikell, I don't understand what you are trying to do. You are matching variable numbers of spaces and word characters. The OP didn't ask for that.

Edit

Reading again, I see that the OP doesn't want an exact match (case insensitivity). This request is not clear to me.

The following code should have arraysearch fail both tests.

While "Health" is contained within both rows they are not exact matches.

Edited by czardas
Link to post
Share on other sites

I believe this _ArraySearch example using a regular expression finds all occurrences of the $sSearch string in "Col 1" of the array.

#include <Array.au3>

Local $aValues[3][2] = [["JobTitle1", "Health Advisor, Aide Health"],["JobTitle2", "Health Assistant, Health Advisor"],["JobTitle3", "Health Assistant, Health"]]
Local $sSearch = "Health Advisor" ; "Health Assistant" ; "Health" ;
For $i = 0 To UBound($aValues) - 1
    $i = _ArraySearch($aValues, '(^|,\h*)(' & $sSearch & ')(,|$)', $i, 0, 0, 3, 1, 1)
    If ($i < 0) Then
        ExitLoop
    Else
        MsgBox(0, "", 'Found :"' & $aValues[$i][1] & '" @ $aValues[' & $i & '][1]')
    EndIf
Next
_ArrayDisplay($aValues)
Link to post
Share on other sites
  • Developers

Are we sure the Array is properly initialized(Populated) before trying to get some sort of work-around for this?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

czardas,

I agree, this "exact match" notion is not completely clear, so there is a bit of interpretation to do  :)

For instance, in the array cell content "Health Assistant, Health Aide", the terms "Health Assistant" and "Health Aide" must match, but not "Health" or "Assistant"

That's why I managed this using a regex to fit these requirements, including the possibility of erratic white spaces

Considering also that only a true/false return is wanted, matching an "extended" pattern works nice - please see my comment in post #18

Malkey,

I really believe that using _ArraySearch is a too heavy way

#include <Array.au3>

Local $aValues[3][2] = [["JobTitle1", " Health Advisor , Aide Health"],["JobTitle2", "Health Assistant ,  Health Advisor "],["JobTitle3", " Health Assistant , Health"]]
Local $sSearch = "Health   Advisor" ; "Health Assistant" ; "Health" ;

$sSearch = StringRegExpReplace($sSearch, '\h+', '\\h+')
For $i = 0 To UBound($aValues) - 1
   If StringRegExp($aValues[$i][1], '(?i)(^|,)\h*' & $sSearch & '\h*(,|$)') Then _
        MsgBox(0, "", 'Found :"' & $aValues[$i][1] & '" @ $aValues[' & $i & '][1]')
Next
_ArrayDisplay($aValues)

Edit typos

Edited by mikell
Link to post
Share on other sites

I understand, but the rules are not clearly defined. Should 'Not Health Advisor' be a match, or 'Advisor for Health'? It seems the OP does not want an exact match, and without knowing what constitutes a match, this is simply a guessing game.

For instance, in the array cell content "Health Assistant, Health Aide", the terms "Health Assistant" and "Health Aide" must match, but not "Health" or "Assistant"

 

This appears at first glance to be a contradiction. From what I can gather it seems you are treating it as if you only want to match the search term as a substring, and not otherwise - basically an inexact match. But how inexact can it be?

Edited by czardas
Link to post
Share on other sites

From what I can gather it seems you are treating it as if you only want to match the search term as a substring, and not otherwise - basically an inexact match. But how inexact can it be?

 

You pointed it out precisely, the search term(s) must be treated as a substring with *requirements* - meaning not less not more than the whole substring in the cell content part(s) limited by start, comma(s) and end ... checking extra spaces in the regex being only an additional precaution in case of WS typos (the needed accuracy degree is left at the discretion of the OP)

A more explicit approach could be the use of StringSplit($aValues[$i][1], ",") before checking, then StringStripWS and finally the "==" test  :)

Edited by mikell
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...