Jump to content

Recommended Posts

Posted

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?

Posted

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

Posted

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.

Posted (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 by Steveiwonder

They call me MrRegExpMan

Posted

Loops killed my father. Well hurt him badly. OKhurthisfeelingsstill.

lol.

He probably hurt his fingers by not using enuff loops. :x

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...