Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Syntax check for If A_loopfield contains var1 AND var2


  • Please log in to reply
3 replies to this topic
  • Guests
  • Last active:
  • Joined: --
Is this sytax correct? I want A_loopfield to contain BOTH var1 and var2 in the same line of a textfile.

Loop, Parse, contents,`n
    {
    If A_LoopField contains %var1%%var2%

Thanks!

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
The easiest way to know if it works is to test it... ;-)
I doubt it works, a way to do this is:
Loop, Parse, contents,`n
{
   If A_LoopField contains %var1%
      If A_LoopField contains %var2%
         ; OK!

Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
You can use the InStr() function in an expression: If InStr(A_LoopField, var1) and InStr(A_LoopField, var2)

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I was curious to see what is the fastest solution, so I made a micro-benchmark: I took a 300KB log file, and I searched lines containing both "java" and "Exception" words (15 lines out of 2045).
I loop 100 times to get significant results.

contains: 1.328000 seconds
InStr: 0.938000 seconds
RegExMatch 1: 1.219000 seconds
RegExMatch 2: 4.937000 seconds
RegExMatch 3: 4.953000 seconds
RegExMatch 4: 1.969000 seconds

SetBatchLines, -1
Process, Priority, , R
FileRead lines, E:\Tests\testimport140c-sp2\tools_testimport_Bif\TestSetAlarm\log\Proptima.200609151137.log

timing =
t1 := A_TickCount
Loop 100
Loop Parse, lines, `n, `r
{
	If A_LoopField contains java
		If A_LoopField contains Exception
			r = %r%%A_LoopField%`n
}
t2 := A_TickCount
timing := timing . "contains: " . (t2 - t1) / 1000 . " seconds`n"

r =
t1 := A_TickCount
Loop 100
Loop Parse, lines, `n, `r
{
	If InStr(A_LoopField, "java") and InStr(A_LoopField, "Exception")
		r = %r%%A_LoopField%`n
}
t2 := A_TickCount
timing := timing . "InStr: " . (t2 - t1) / 1000 . " seconds`n"

r =
t1 := A_TickCount
Loop 100
Loop Parse, lines, `n, `r
{
	If (RegExMatch(A_LoopField, "java.*Exception") > 0)
		r = %r%%A_LoopField%`n
}
t2 := A_TickCount
timing := timing . "RegExMatch 1: " . (t2 - t1) / 1000 . " seconds`n"

r =
t1 := A_TickCount
Loop 100
Loop Parse, lines, `n, `r
{
	If (RegExMatch(A_LoopField, "java.*Exception|Exception.*java") > 0)
		r = %r%%A_LoopField%`n
}
t2 := A_TickCount
timing := timing . "RegExMatch 2: " . (t2 - t1) / 1000 . " seconds`n"

r =
t1 := A_TickCount
Loop 100
Loop Parse, lines, `n, `r
{
	If (RegExMatch(A_LoopField, "Exception.*java|java.*Exception") > 0)
		r = %r%%A_LoopField%`n
}
t2 := A_TickCount
timing := timing . "RegExMatch 3: " . (t2 - t1) / 1000 . " seconds`n"

r =
t1 := A_TickCount
Loop 100
Loop Parse, lines, `n, `r
{
	If (RegExMatch(A_LoopField, "java.*Exception") > 0 || RegExMatch(A_LoopField, "Exception.*java") > 0)
		r = %r%%A_LoopField%`n
}
t2 := A_TickCount
timing := timing . "RegExMatch 4: " . (t2 - t1) / 1000 . " seconds`n"

MsgBox %timing%
Note that in my case, RegExMatch 1 is incorrect, as it doesn't report some lines. But it is efficient if you are sure of the order of the strings. In all cases, InStr is the fastest, which is rather logical.
It also shows that performing two regex searches is faster than doing a too complex one...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")