cag8f Posted December 13, 2010 Posted December 13, 2010 Say I have 100 names stored in an array, and I want to search a large text file line by line for one of those names in the line, and perform operations if one is found. The line will always have the same format. Is there any way to do that without a loop? i.e. something like: If StringRegExp($line, "text" & <any name in the array> & "more text") Then Do stuff EndIf Does something like this syntax exist? Or do I need to somehow loop through the array to accomplish what I want? Now that I think about it, looping wouldn't be too tough. But for future reference, can I accomplish without looping?
Steveiwonder Posted December 13, 2010 Posted December 13, 2010 Something like this maybe. I'm not the best with Reg's but something like this should work. There is a loop in there but still if your looking for a starting point. Dim $names[4] = ["Sue","Steve", "Stuart","Sally"] $file = FileOpen("t.txt") while 1 $line = FileReadLine($file) If @error then ExitLoop if StringRegExp($line, CompilePattern($names)) == 1 Then ConsoleWrite($line & " : Match" & @CRLF) EndIf WEnd Func CompilePattern($arr) Dim $result For $name in $arr $result &= $name & "|" Next $result = StringTrimRight($result, 1) Return "(.*)(?i:" & $result & ")(.*)" EndFunc My t.txt contained Line1 has Sue Line2 has noone line3 has sally with lowercase line4 has noone line5 has noone line6 has stuart and steve They call me MrRegExpMan
MvGulik Posted December 13, 2010 Posted December 13, 2010 without a loop? What do you have against loops? "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ...
cag8f Posted December 13, 2010 Author Posted December 13, 2010 Loops killed my father. Well hurt him badly. OKhurthisfeelingsstill. Thanks for the sample script. I'm a noob though and have never seen a loop in the form: For $name in $array Next What is that doing exactly? I assume it is looping over the array. $name is one of the entries in the array? The loop starts at that name (the first instance of that name?) and works its way up the array? I'll end up using a loop, since even some built in function will probably utilize a loop in some fashion.
Steveiwonder Posted December 13, 2010 Posted December 13, 2010 (edited) Yes, $name will be one single element of an array. Then, Next happens. So $name becomes the $next element in the array. They are also referred to as a Foreach loop in some languages. ForEach $name in $names - Sounds a little more like english and more understandable. When i was learning i always thought of it like this. Edited December 13, 2010 by Steveiwonder They call me MrRegExpMan
cag8f Posted December 13, 2010 Author Posted December 13, 2010 OK I see it in the help file now. The For...In...Next loop. Overlooked it earlier, thanks.
MvGulik Posted December 13, 2010 Posted December 13, 2010 Loops killed my father. Well hurt him badly. OKhurthisfeelingsstill.lol.He probably hurt his fingers by not using enuff loops. "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ...
James Posted December 13, 2010 Posted December 13, 2010 He probably hurt his fingers by not using enuff loops. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ
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