mortog Posted July 20, 2013 Share Posted July 20, 2013 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 More sharing options...
mikell Posted July 20, 2013 Share Posted July 20, 2013 $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 Link to comment Share on other sites More sharing options...
Developers Jos Posted July 20, 2013 Developers Share Posted July 20, 2013 (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 July 20, 2013 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 More sharing options...
czardas Posted July 20, 2013 Share Posted July 20, 2013 (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 July 20, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
mortog Posted July 20, 2013 Author Share Posted July 20, 2013 (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 July 20, 2013 by mortog Link to comment Share on other sites More sharing options...
mikell Posted July 20, 2013 Share Posted July 20, 2013 Yes... I answered to the question litterally but a "11" will give a positive result, which is probably not expected Link to comment Share on other sites More sharing options...
mortog Posted July 20, 2013 Author Share Posted July 20, 2013 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. Link to comment Share on other sites More sharing options...
mikell Posted July 20, 2013 Share Posted July 20, 2013 OK... in this case the Czardas way is the best one as it returns a boolean 'true/false' result Link to comment Share on other sites More sharing options...
Developers Jos Posted July 20, 2013 Developers Share Posted July 20, 2013 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 More sharing options...
czardas Posted July 20, 2013 Share Posted July 20, 2013 (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 July 20, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted July 20, 2013 Share Posted July 20, 2013 (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 July 20, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
mikell Posted July 20, 2013 Share Posted July 20, 2013 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) antonioj84 and czardas 2 Link to comment Share on other sites More sharing options...
mortog Posted July 21, 2013 Author Share Posted July 21, 2013 (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 July 21, 2013 by mortog Link to comment Share on other sites More sharing options...
czardas Posted July 21, 2013 Share Posted July 21, 2013 (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 July 21, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now