Page 1 of 1

if contains problem code example included

Posted: 20 Jul 2018, 18:31
by AHKStudent
See in my code here, I just want to check if the line contains a word, but it does not work

Code: Select all

dummytext =
(
first line
second line
third line
forth line
)

Loop, parse, dummytext, `n, `r
{

      if (A_LoopField contains "second") ; if I change this to: if (A_LoopField = "second line") everything works
      {
         continue
      }
   tlines%A_Index% := A_LoopField 
   thevars .= "tlines" A_Index " = " tlines%A_Index% "`n"
   tht = %a_index%
}

MsgBox, %thevars% `n total loops %tht%
ExitApp

Re: if contains problem code example included  Topic is solved

Posted: 20 Jul 2018, 18:37
by gregster
Well, the docs say, contains is not supported in expressions. Hence, I guess, the () with If won't work. But you don't need it. And of course, the Matchlist takes "s literally. So...

Code: Select all

dummytext =
(
first line
second line
third line
forth line
)

Loop, parse, dummytext, `n, `r
{

      if A_LoopField contains second ; if I change this to: if (A_LoopField = "second line") everything works
      {
         continue
      }
   tlines%A_Index% := A_LoopField 
   thevars .= "tlines" A_Index " = " tlines%A_Index% "`n"
   tht = %a_index%
}

MsgBox, %thevars% `n total loops %tht%
ExitApp

Re: if contains problem code example included

Posted: 20 Jul 2018, 18:48
by AHKStudent
thank you gregster

Re: if contains problem code example included

Posted: 20 Jul 2018, 18:55
by gregster
You are welcome!
I just noticed that if the last line of the dummytext contains second, this last iteration won't be counted for the total loops (because Continue will end the loop in this case, before the tht variable can be updated... you could probably move this last line of the loop to the beginning of the loop in order to fix that (if it is important).