Jump to content

Finding a string in an array


Docfxit
 Share

Recommended Posts

I have a field $asFields[5] in a record that looks like this: "BAJA FRESH MEXI GRILL VALENCIA CA "

I need to search an array that I loaded called $Vendor that looks like this:

[1]|Vendors

[2]|CHEVRON

[3]|BAJA FRESH

[4]|GRAND PANDA

[5]|LENOVO

When it finds Baja Fresh I need to know the index so I can use an alternating array.

Func ReadRecord()   ;  Read the transaction records
    While $fResult = 1
        Global $sString = FileReadLine($hQBTrans)
            If @error = -1 Then 
                ;End of file reached
                ExitLoop
            EndIf
        $asFields = StringRegExp($sString, $sPattern, 3)
        _ArrayDisplay($asFields, "Input Fields")                                                        
            If $asFields[0] = "TRNS" Then 
                $Pos = _ArraySearch ($Vendor, $asFields[5], 0, 0, 0, True)
                Select
                    Case $Pos = -1
                        MsgBox(0, "Not Found", '"' & $asFields[5] & '" was not found in the array.')
                    Case Else
                        MsgBox(0, "Found", '"' & $asFields[5] & '" was found in the array at pos ' & $Pos & ".")
                    EndSelect
            EndIf
            MsgBox(4096, "Record", "InputString - " & $sString & @LF & "Result - " &$fResult)
    WEnd            
EndFunc

How can I find the vendor strings?

Thank you,

Docfxit

Edited - Added _ArraySearch

Edited by docfxit
Link to comment
Share on other sites

Hi,

_ArraySearch ?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Hi,

_ArraySearch ?

So long,

Mega

Thank you for pointing me in the right direction. I added _ArraySearch to my sample above. The problem with what I have is I need it to come up with a match on part of the string. Right now it's looking for the entire string in the $Vendors array. In this example I need to have a hit on BAJA FRESH.

Thank you,

Docfxit

Link to comment
Share on other sites

Hi,

there is a parameter called $fPartialSearch. Does this not help?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Hi,

there is a parameter called $fPartialSearch. Does this not help?

So long,

Mega

I tried it with $fPartialSearch = true and it didn't find a match. I'm not sure how it could though.

The string I am searching with (What to search) is "BAJA FRESH MEXI GRILL VALENCIA CA " Note: There will be a file of about 200 records.

The array I am searching in is:

[1]|Vendors

[2]|CHEVRON

[3]|BAJA FRESH

[4]|GRAND PANDA

[5]|LENOVO

Note: The vendor array will be about 100 to start and it will grow.

The only way I could see this working is if I sequenced through the entire vendor array for each and every record. That way the Vendor would be What to search for and the field $asFields[5] would be What to search in. I'm guessing that would be some kind of string search instead of an arraysearch.

Thank you,

Docfxit

Link to comment
Share on other sites

I also posted a function to find all occurences of a search string in an array so Baja will return all elements containing the string Baja

http://www.autoitscript.com/forum/index.php?showtopic=51629

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...