What the hell is happening with InStr?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
foxdanger
Posts: 83
Joined: 11 Jun 2019, 13:48

What the hell is happening with InStr?

Post by foxdanger » 11 Jun 2021, 17:16

I have this inside a loop:

Code: Select all

test := InStr(A_Loopfield, "USER")
msgbox %test%
For each line of the loop, it finds different results because each line has or not the string USER

So, when the line doesnt has USER it returns 0.

It works... BUT... When I try to use a variable (which is a string) on the IF, it ONLY RETURNS 1 all the time.

Code: Select all

variable := "USER"
test := InStr(A_Loopfield, %variable)
msgbox %test%
This not work, only returns 1...

What is happening here?
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: What the hell is happening with InStr?

Post by boiler » 11 Jun 2021, 17:48

What the hell is happening is that it is a function, and function parameters are always expressions, so remove the %. (That implementation with a single % and no space would not be correct in any case, by the way.)
foxdanger
Posts: 83
Joined: 11 Jun 2019, 13:48

Re: What the hell is happening with InStr?

Post by foxdanger » 11 Jun 2021, 17:52

boiler wrote:
11 Jun 2021, 17:48
What the hell is happening is that it is a function, and function parameters are always expressions, so remove the %. (That implementation with a single % and no space would not be correct in any case, by the way.)
The single % was a misstype. On the code of course I'm using both %.

But thx for the answer, so I should use the variable name directly without the %% right?

Thx a lot my friend
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: What the hell is happening with InStr?

Post by boiler » 11 Jun 2021, 17:55

Yes, with expressions, you don’t use % around variables unless you have a specific purpose for doing so in which the variable contains the name of another variable which contains the contents to be referenced (known as a double dereference).
foxdanger
Posts: 83
Joined: 11 Jun 2019, 13:48

Re: What the hell is happening with InStr?

Post by foxdanger » 11 Jun 2021, 18:01

boiler wrote:
11 Jun 2021, 17:48
What the hell is happening is that it is a function, and function parameters are always expressions, so remove the %. (That implementation with a single % and no space would not be correct in any case, by the way.)


Man, thx it worked. I have another problem now:

Code: Select all

Loop, parse, A_LoopReadLine, %A_Tab%
        {
            if(InStr(A_Loopfield, _resolutionAndScale) == 0){
                flag := "true"
                msgbox AQUI
                continue
            }
            MsgBox "line " %line%
            line++
            MsgBox "Flag Is " %flag%
            }
This after 3th attempt enters on the IF and should skip the msgbox after the if, but it dont.

Is CONTINUE to skip the rest of the iteration right? So, why is not skiping the rest and going to the next iteration?
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: What the hell is happening with InStr?

Post by boiler » 11 Jun 2021, 18:14

Are you sure it’s not just displaying that on the next iteration instead of on the same iteration? Here’s how you can check:

Code: Select all

Loop, parse, A_LoopReadLine, %A_Tab%
        {
            if(InStr(A_Loopfield, _resolutionAndScale) == 0){
                flag := "true"
                msgbox AQUI
                MsgBox Inside "if" block on iteration %A_Index%
                continue
            }
            MsgBox "line " %line%
            line++
            MsgBox "Flag Is " %flag%
            MsgBox, After "if" block on iteration %A_Index%
            }
See if the iteration number reported by the two MsgBoxes are the same or not.
foxdanger
Posts: 83
Joined: 11 Jun 2019, 13:48

Re: What the hell is happening with InStr?

Post by foxdanger » 12 Jun 2021, 07:24

boiler wrote:
11 Jun 2021, 18:14
Are you sure it’s not just displaying that on the next iteration instead of on the same iteration? Here’s how you can check:

Code: Select all

Loop, parse, A_LoopReadLine, %A_Tab%
        {
            if(InStr(A_Loopfield, _resolutionAndScale) == 0){
                flag := "true"
                msgbox AQUI
                MsgBox Inside "if" block on iteration %A_Index%
                continue
            }
            MsgBox "line " %line%
            line++
            MsgBox "Flag Is " %flag%
            MsgBox, After "if" block on iteration %A_Index%
            }
See if the iteration number reported by the two MsgBoxes are the same or not.
The %A_index% returns always 1. But I understand what I did wrong. I put the incrementation only out of the if, and I was thinking the line was wrong. You're right, the continue was working. Thx.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: What the hell is happening with InStr?

Post by boiler » 12 Jun 2021, 07:29

Not only does continue work as expected, but A_Index does not always return 1, as this demonstration of that code indicates. You must have something else in your code that you are not yet understanding.

Code: Select all

line := 99
ReadLine := "red	green	blue"
_resolutionAndScale := "g"

Loop, parse, ReadLine, %A_Tab%
        {
            if(InStr(A_Loopfield, _resolutionAndScale) == 0){
                flag := "true"
                msgbox AQUI
                MsgBox Inside "if" block on iteration %A_Index%
                continue
            }
            MsgBox "line " %line%
            line++
            MsgBox "Flag Is " %flag%
            MsgBox, After "if" block on iteration %A_Index%
            }
foxdanger
Posts: 83
Joined: 11 Jun 2019, 13:48

Re: What the hell is happening with InStr?

Post by foxdanger » 12 Jun 2021, 07:54

boiler wrote:
12 Jun 2021, 07:29
Not only does continue work as expected, but A_Index does not always return 1, as this demonstration of that code indicates. You must have something else in your code that you are not yet understanding.

Code: Select all

line := 99
ReadLine := "red	green	blue"
_resolutionAndScale := "g"

Loop, parse, ReadLine, %A_Tab%
        {
            if(InStr(A_Loopfield, _resolutionAndScale) == 0){
                flag := "true"
                msgbox AQUI
                MsgBox Inside "if" block on iteration %A_Index%
                continue
            }
            MsgBox "line " %line%
            line++
            MsgBox "Flag Is " %flag%
            MsgBox, After "if" block on iteration %A_Index%
            }
Yeah, I don't know why but the %A_index% is returning always 1. But now is everything working fine.

I have one more doubt:

The IniWrite takes a while to write the file. 2 or 3 seconds because is big.

THere's some function or call that tells me the write ends? So I could make a "wait a little" screen to block the user make something during the write?
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: What the hell is happening with InStr?

Post by boiler » 12 Jun 2021, 08:04

foxdanger wrote: Yeah, I don't know why but the %A_index% is returning always 1. But now is everything working fine.
Then it must really be in the first iteration of the loop. A_Index doesn't lie. Perhaps it is producing the output you wanted, but how it's doing that must not be how you're expecting it to work if it's doing it all within the first iteration of the loop.

foxdanger wrote: The IniWrite takes a while to write the file. 2 or 3 seconds because is big.

THere's some function or call that tells me the write ends? So I could make a "wait a little" screen to block the user make something during the write?
You could make a loop in which it uses IniRead to keep checking to see if the ini file contains the new contents, then remove the "wait a little" screen after it's confirmed that the file has been updated.
foxdanger
Posts: 83
Joined: 11 Jun 2019, 13:48

Re: What the hell is happening with InStr?

Post by foxdanger » 12 Jun 2021, 08:54

boiler wrote:
12 Jun 2021, 08:04
foxdanger wrote: Yeah, I don't know why but the %A_index% is returning always 1. But now is everything working fine.
Then it must really be in the first iteration of the loop. A_Index doesn't lie. Perhaps it is producing the output you wanted, but how it's doing that must not be how you're expecting it to work if it's doing it all within the first iteration of the loop.

foxdanger wrote: The IniWrite takes a while to write the file. 2 or 3 seconds because is big.

THere's some function or call that tells me the write ends? So I could make a "wait a little" screen to block the user make something during the write?
You could make a loop in which it uses IniRead to keep checking to see if the ini file contains the new contents, then remove the "wait a little" screen after it's confirmed that the file has been updated.
Thank you :D
Post Reply

Return to “Ask for Help (v1)”