Jump to content

How to search if a string contains an array value


Recommended Posts

Hey folks,

I'm a bit stuck on the following: I've got a string of text and an array that I want to compare (I want to check if any of the values in the array exist in the string), but I can't get it to work. I also want this to work in a While loop.

 

I've been thinking of a way to give an example of what I'm trying to do, but I don't know how I'd approach this using AutoIt. Let's say for arguments sake that I have a $string = "1,2,3,4,5,6,7,8,9" and Global $array[3] = ["1", "3", "6", "9"]

My question is then : how do I search if any of the values in the array (so 1,3,6,or 9) exist in the string? I don't necessarily need a return of which of these values exists in the string, so long as any of them does. In a sense you could say I'm trying to do the exact opposit of what an arraysearch does.

Thanks a lot in advance for the help.

Link to comment
Share on other sites

  • Developers

This example is far from being foolproof. You probably need to StringSplit the string into an array and then loop through one array searching for each value in the other array using _ArraySearch().

Jos

Edited by 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 comment
Share on other sites

Local $array[4] = ["1", "3", "6", "9"] ; added an extra element to the declration
Local $string = "1,2,3,4,5,6,7,8,9"
Local $bFound = False

For $i = 0 To UBound($array) -1 ; Test from element 0 to the last element of the array
    If StringInStr($string, $array[$i]) Then
        $bFound = True
        ExitLoop ; Unless you need to find more instances we exit this part of the code imediately
        ; Otherwise the example needs to be rewritten to do exactly what you want it to do.
    EndIf
Next
MsgBox(0, "Return", "Found = " & $bFound)

;

Edit : I forgot to subtract 1 from Ubound.

Same method as mikell used.

Edited by czardas
Link to comment
Share on other sites

That's what I was thinking of at first as well Jos, but that seemed to be a bit too extensive for the relatively easy thing I was trying to do. Also I wasn't to concerned about possible problems since I really didn't want anything else then to search a specific string (in this case the contents of a text file) for various array values.

Mike, your suggestion did exactly what I was looking for and works like a charm, thanks a lot :)

Edit: just tried yours also Czardas, works like a charm as well.

Edited by mortog
Link to comment
Share on other sites

  • Developers

That script has the same issue.

try something like this:

#include<array.au3>
$string = "1,2,3,4,5,7,8,9"
$array2 = StringSplit($string,",",2)
Dim $array[4] = ["1", "3", "6", "9"]
For $i = 0 to UBound($array)-1
    If _ArraySearch($array2,$array[$i])> -1 Then
        ConsoleWrite($array[$i] & "  exists in the string" & @CRLF)
    Else
        ConsoleWrite($array[$i] & "  doesn't exists in the string" & @CRLF)
    EndIf
Next

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 comment
Share on other sites

I find searching strings seems generally faster. Here's a method I use. Look at the changes. It is not universal, but that can also be introduced. The comma delimiter should not be part of the search pattern (and also not part of an array element) If it is then you need to work around this somehow. I always make sure the delimiter does exactly what it's meant to do - separate the values we are interested in testing. Then it will be foolproof.

;

Local $array[4] = ["1", "3", "6", "9"] ; added an extra element to the declration
Local $string = "1,2,3,4,5,6,7,8,9"
$string = "," & $string & "," ; modified example
Local $bFound = False

For $i = 0 To UBound($array) -1 ; Test from element 0 to the last element of the array
    If StringInStr($string, "," & $array[$i] & ",") Then ; modified search criteria
        $bFound = True
        ExitLoop ; Unless you need to find more instances we exit this part of the code immediately
        ; Otherwise the example needs to be rewritten to do exactly what you want it to do.
    EndIf
Next
MsgBox(0, "Return", "Found = " & $bFound)

;

This will now work for 11 etc... Thanks Jos for pointing out the limitations.

Edited by czardas
Link to comment
Share on other sites

It looks like this choice was mortog's

I agree with searching strings, so regex should work too

Local $array[4] = ["1", "3", "6", "9"]
Local $string = "1,2,3,4,5,6,7,8,9"
Local $bFound = False

For $i = 0 To UBound($array)-1 
   If StringRegExp($string, '(^|,)' & $array[$i] & '(,|$)') Then 
        $bFound = True
        ExitLoop 
    EndIf
Next
MsgBox(0, "Return", "Found = " & $bFound)
Link to comment
Share on other sites

The commas were just as an example, in the actual string and array that I'll be using there will be whole sentenses so I should not run into the 11 issue.

I went for your latest changes though as I'd otherwise run into other problems. Thanks again!

Edited by mortog
Link to comment
Share on other sites

If you know a delimiter that you can use which will not form part of the substrings or any element within the array, it should be easy. If you do not know the contents of the array, you will have to test all the elements to make sure your delimiter is safe to use. With the method posted by Jos this will never be a problem. The question is how important is it that you have the highest speeds possible. The code can become overly complicated for it to be worth saving a second or two. With sentences, comma would clearly be a terrible choice for a delimiter.

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