Sign in to follow this  
Followers 0
mortog

How to search if a string contains an array value

14 posts in this topic

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.

Share this post


Link to post
Share on other sites



$string = "1,2,3,4,5,6,7,8,9"
Dim $array[4] = ["1", "3", "6", "9"]
For $i = 0 to UBound($array)-1
 If StringInStr($string, $array[$i]) Then Msgbox(0,"", $array[$i] & "  exists in the string")
Next

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Yes... I answered to the question litterally but a "11" will give a positive result, which is probably not expected

Share this post


Link to post
Share on other sites

Ah, you're right about that.. but in the string and array that I'll be using in reality there's never going to be a 2 digit number, so that shouldn't pose an issue.

Share this post


Link to post
Share on other sites

OK... in this case the Czardas way is the best one as it returns a boolean 'true/false' result

Share this post


Link to post
Share on other sites

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

OK... in this case the Czardas way is the best one as it returns a boolean 'true/false' result

 

It's an arbitrary choice.

Edited by czardas

Share this post


Link to post
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)
2 people like this

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

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

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  
Followers 0