Check if variable has something

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Check if variable has something

06 Aug 2018, 14:17

I know how to check if variable is empty

if (foo = "") or if (foo != "") or even if !(foo = "")
https://autohotkey.com/board/topic/7070 ... snt-blank/

How do I check if variable has data

My case is that if variable is empty I want my script to go on, I don't want to create a gosub, label etc

So I am hoping to get something like

if variable has data do the following

but if it does the script just moves on.

You can also suggest if you like something else to do so the script keeps moving along other than gosub

so

Code: Select all

if (foo != "")
{
keep moving ; what is this keep moving command? 
}
else
{
do these commands
}
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Check if variable has something

06 Aug 2018, 14:42

I'm not understanding the problem here. There is no "keep moving" command. After your If statement when the variable is null, it executes a block of code; when the variable is not null, it executes the Else block of code. Both of those paths, in essence, represent the "keep moving" aspect of the script, if you want to call it that. It's no different from any other If statement, or If/Else pair, or Try/Catch pair, or any conditional statement. Scripts "keep moving" by executing the appropriate paths based on the outcomes of conditional statements — and, of course, sequentially, when there are no conditional statements or other statements that cause a change in control flow, such as Gosub, Goto, and function calls. Regards, Joe
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if variable has something

06 Aug 2018, 15:02

- I too would like there to be an official 'do nothing' one-liner.
- You can use two curly braces:

Code: Select all

if (foo != "")
{
}
- Or Sleep 0:

Code: Select all

if (foo != "")
	Sleep, 0
- If there is something better than Sleep 0 I would like to know. It is not a perfect NoOp:
Sleep - Syntax & Usage | AutoHotkey
https://www.autohotkey.com/docs/commands/Sleep.htm
A delay of 0 yields the remainder of the script's current timeslice to any other processes that need it (as long as they are not significantly lower in priority than the script). Thus, a delay of 0 produces an actual delay between 0 and 20ms (or more), depending on the number of needy processes (if there are no needy processes, there will be no delay at all).
- Alternatively, you might be interested in the 'continue' and 'break' statements in a Loop/for loop/while loop. Or the 'return' statement in a function.
Last edited by jeeswg on 01 Aug 2019, 04:50, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Check if variable has something

06 Aug 2018, 15:13

jeeswg wrote:official 'do nothing' one-liner
in what world would that make any sense at all?
jeeswg wrote:

Code: Select all

if (foo != "")
	Sleep, 0
this is obviously doing something
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if variable has something

06 Aug 2018, 15:30

- I showed a two-liner that does nothing.
- I'm asking for a one-liner that does nothing. What's the best currently available option?
- A do nothing function can be useful for complicated if-else ladders.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Check if variable has something

06 Aug 2018, 15:33

jeeswg wrote:I too would like there to be an official 'do nothing' one-liner.
I didn't read the question that way, but I could certainly be wrong about that.
swagfag wrote:in what world would that make any sense at all?
In the world of assembly language programming. I used it often in my mainframe kernel OS dev days. It is known as a "no-op" (no operation) — the NOP instruction in Intel x86 architecture; NOP and NOPR in IBM 360 architecture. The no-op could be critical in OS assembly language code, but seems to me unnecessary in the AHK world, except perhaps as a placeholder (stub) for future code. Many other ways to achieve that, of course. Regards, Joe
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Check if variable has something

06 Aug 2018, 16:00

thank you all for the help, was able to understand a little more of the logic behind this
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Check if variable has something

06 Aug 2018, 16:04

jeeswg wrote:A do nothing function can be useful for complicated if-else ladders.
I don't see that. I've done some pretty complicated nested If/Else statements in AHK programs, but don't recollect the need for a no-op in any of them (or when using other high-level languages). If you really needed it you could, of course, do var:=var, or temp:=0, or call a function noop() that just does a Return, or many other constructs that, in essence, do nothing — except take time. But I'm still hard-pressed to see the need in AHK. Regards, Joe
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Check if variable has something

07 Aug 2018, 03:37

- I'm asking for a one-liner that does nothing. What's the best currently available option?

Code: Select all

;
or with your definition of doing nothing,

Code: Select all

IfNotEqual, Var,,sleep 0
this is obviously doing something
indeed.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Check if variable has something

07 Aug 2018, 06:31

JoeWinograd wrote:In the world of assembly language programming. I used it often in my mainframe kernel OS dev days. It is known as a "no-op" (no operation) — the NOP instruction in Intel x86 architecture; NOP and NOPR in IBM 360 architecture. The no-op could be critical in OS assembly language code
NOPS can be used to properly align Pointers that get accessed a lot - accessing an aligned pointer is a lot faster. In AHK it's only used when we use MCode or DLLs.

@Topic to come back to the topic:
There is never a need to place empty beaches for anything in AHK.
Checking if a variable is empty or not empty would look like this in my code:

Code: Select all

if var ;is true if var is anything but "" 0 or 0.0
if !var ;the opposite of the one above
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Check if variable has something

07 Aug 2018, 07:06

If I understand your question right:

Code: Select all

If (Condition) ; True
{
   Do Nothing
}
Else ; False
{
   Do Something
}
is equivalent to

Code: Select all

If !(Condition) ; False
{
   Do Something
}
You never need an If or Else branch doing nothing.

If you swap the order it's more obvious

Code: Select all

If (Condition)
{
   Do Something
}
Else
{
   Do Nothing
}
Who woulf code such an Else branch?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Check if variable has something

07 Aug 2018, 09:35

Hello :wave:.
just me, complicated if-else ladders were mentioned, consider

Code: Select all

If (Condition)
{
   Do Something
}
Else if (Condition2)
{
   Do Nothing
}
Else if (Condition3)
{
   Do Something else
}
Else if (Condition4)
{
   Do Something else
}
Logically, this can ofc be replaced by

Code: Select all

If (Condition)
{
   Do Something
}
Else if !(Condition2)
{
	if (Condition3)
	{
		Do Something else
	}
	Else if (Condition4)
	{
		Do Something else
	}
}
In some situations, the first version might be preferable, for some reason. But in such a case, having a do_nothing statement or function is not very useful, so instead of,

Code: Select all

Else if (Condition2)
{
   Do_Nothing()
}
you put a ; comment describing why no action is taken here, and you do certainly not put sleep 0 there either.

My own reasons for sometimes ending up having the first construct, most likely is me making some mistake, where I later realise I do not need to take an action under the condition, but still, if the condition is true, the other conditions need not be considered, so I delete the code in that branch and make a comment.

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if variable has something

21 Aug 2018, 00:34

- @Helgef: Nice examples, thanks.

- @JoeWinograd: Thanks for mentioning 'noop', here's a nice article on how it's done in different languages:
NOP - Wikipedia
https://en.wikipedia.org/wiki/NOP

- I once created a custom NoOp function for some advanced debugging. I replaced all of the functions in a script with a function that does nothing.
- (The function was variadic, it accepted an unlimited number of parameters so that there wouldn't be any parameter count errors.)
Error: Too few parameters passed to function.
Error: Too many parameters passed to function.
- [EDIT:] Here's another example I found, although it's only needed because of something quirky in AHK v1.

Code: Select all

IfMsgBox, OK
	Sleep, 0
else
	return
- Having a named function makes the concept consistent, and thus searchable across scripts.
- Plus, unlike some other approaches, it is a one-liner.
- It's easier to search for than a lone semicolon, or { and }, which are virtually impossible to search for (a lot of filtering and manual checking required).
- It can't be misspelled or else the script wouldn't run.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if variable has something

29 Nov 2018, 19:05

I'm surprised no-one mentioned {} as a one-liner. wolf_II mentioned it here:
terminology: function definition line - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=76&t=59463&p=250559#p250559
Example script:

Code: Select all

q:: ;NoOp via {}
Loop, 2
{
	MsgBox, 1
	if (A_Index = 1)
		{}
	else
		MsgBox, 2
}
return
Also, there's a good thread about NoOp, here:
NOOP (No Operation) - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/12707-noop-no-operation/
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if variable has something

31 Jul 2019, 12:25

A good example from here:
jeeswg's File object mini-tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=74&t=66674

With NoOp:

Code: Select all

;identify the encoding of a txt file
vPath := A_ScriptFullPath
vEnc := ""
if (oFile := FileOpen(vPath, "r"))
{
	vEnc := oFile.Encoding ;e.g. CP1252/UTF-8/UTF-16
	if (SubStr(vEnc, 1, 3) = "UTF")
	{ ;do nothing: vEnc is UTF-8/UTF-16
	}
	else if (oFile.ReadUShort() = 0xFFFE) ;bytes: 'FE FF'
		vEnc := "UTF-16 BE"
	else
		vEnc := "ANSI"
	oFile.Close()
}
MsgBox, % vEnc "`r`n" vPath
return
Without NoOp (much less clear, much less easy to modify, obscures the logic: it's not how the programmer thought of it, it uses different logical steps):

Code: Select all

;identify the encoding of a txt file
vPath := A_ScriptFullPath
vEnc := ""
if (oFile := FileOpen(vPath, "r"))
{
	vEnc := oFile.Encoding ;e.g. CP1252/UTF-8/UTF-16
	if (oFile.ReadUShort() = 0xFFFE) ;bytes: 'FE FF'
		vEnc := "UTF-16 BE"
	else if !(SubStr(vEnc, 1, 3) = "UTF")
		vEnc := "ANSI"
	oFile.Close()
}
MsgBox, % vEnc "`r`n" vPath
return
Also, it's a bit facile to complain about Sleep, 0, of course it is not a perfect NoOp, however, in terms of readability: sleeping for 0 milliseconds is doing nothing. Can anyone think of a more intuitive one-liner, other than braces or a custom function, for 'do nothing'?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Check if variable has something

01 Aug 2019, 02:03

jeeswg wrote:it's a bit facile to complain about Sleep, 0, of course it is not a perfect NoOp, however, in terms of readability: sleeping for 0 milliseconds is doing nothing.
Anyone with the ability to read (the documentation) would disagree. Again,
swagfag wrote:this is obviously doing something
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if variable has something

01 Aug 2019, 04:40

- @Helgef: It seems you didn't read what I said: Sleep, 0 is not a perfect NoOp. And guess what, I based this assertion on this bit of the documentation (which I read):
Sleep - Syntax & Usage | AutoHotkey
https://www.autohotkey.com/docs/commands/Sleep.htm
A delay of 0 yields the remainder of the script's current timeslice to any other processes that need it
- The question is, of the available options, which is the best candidate.

- Potential candidates for a one-line pseudo-NoOp would probably come from one of:
command, control flow statement, directive, function, operator, variable
- Control flow statements/directives wouldn't work. And I wouldn't want any variable assignments to take place (although I might use a built-in variable in some way). So that leaves: command/function and operator.
- Some ideas, in case anyone wishes to state their preference:

Code: Select all

;q:: ;test NoOp options
MsgBox
Sleep, 0
DllCall("kernel32\Sleep", "UInt",0)
SoundBeep,, 0
;0+0 ;causes error (AHK v1/v2)
;0=0 ;causes error (AHK v2), assigns to variable 0 (AHK v1)
;0==0 ;causes error (AHK v2), assigns to variable 0 (AHK v1)
(0) ;the best option?
(0+0)
(0=0)
(0==0)
Abs(0)
;CallbackFree(0) ;causes error (AHK v2)
;Input ;in AHK v2, this does Input until you close the tray icon
;InputEnd()
SetRegView, % A_RegView
MsgBox
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Check if variable has something

01 Aug 2019, 06:38

AHKStudent wrote:
06 Aug 2018, 14:17
I know how to check if variable is empty

if (foo = "") or if (foo != "") or even if !(foo = "")
In the examples you presented, it's if (foo = "") that checks if the variable is empty.

if (foo != "") or if !(foo = "") checks if the variable contains something. Maybe this was confusing you right off the bat.
How do I check if variable has data
if (foo != "") or if !(foo = "") ; if the variable is not empty (thus has something)
My case is that if variable is empty I want my script to go on, I don't want to create a gosub, label etc
The opposite of what your are saying is, if the variable is not empty, do something. Which is the below (like what Just me explained)

Code: Select all

if !(foo = "") ; 
{
   Do Something
}
But you can also pick, if the variable is empty, do something.

Code: Select all

if (foo = "") ; 
{
   Do Something
}
You usually won't need to check if the variable is empty and do nothing, because you can check if it's not empty and do something or check if it' empty and do something.

But if you must, Jeeswg gave you solutions, to do nothing.

Code: Select all

if (foo != "")
{
}

- Or Sleep 0:

if (foo != "")
	Sleep, 0
if variable has data do the following

You can also suggest if you like something else to do so the script keeps moving along other than gosub

Code: Select all

if (foo != "")
{
keep moving ; what is this keep moving command? 
}
else
{
do these commands
}
Your code is arguably demonstrating a problem with logic. You have to flip it, and see the opposite as well, which will be the solution.

You likely want, if not empty, do something...

Code: Select all

if !(foo = "") ; if not empty
{
   Do Something
}
or you can do, if empty, do something

Code: Select all

if (foo = "") ; if empty
{
   Do Something
}
If you are having trouble seeing the opposite to get the solution, you can continue with that thinking pattern and simply have code that does nothing. Nothing wrong with it, but it's arguably wasteful code, because it's not doing anything. But as Jeeswg has mentioned, it can sometimes make things logically clear, as to what you are doing or want to do. It's more important that you understand the code that you write, even when looking at it months or years later, and that your program does what you want. Functionality over style points.

Code: Select all

if (foo != "")
{

}
else
{
	Do Something
}

- Or Sleep 0:

if (foo != "")
{
	Sleep, 0
}
else
{
	Do Something
}
Edited to be more clear.
Last edited by SOTE on 03 Aug 2019, 14:33, edited 4 times in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Check if variable has something

02 Aug 2019, 03:28

It seems you didn't read what I said: Sleep, 0 is not a perfect NoOp. And guess what, I based this assertion on this bit of the documentation (which I read):
I read what you wrote, I'll admit I misinterpreted it if you can confirm that by not perfect you mean not relevant or recommended what so ever in this context. Sleep 0 should be used only for what it is described to do, which actually is something, ie., not nothing. Please also note that,
While sleeping, new threads can be launched via hotkey, custom menu item, or timer.
The important thing is not that you admit sleep 0 is a poor suggestion which you made without any regard to the documentation, that won't happen, but feel free to surprise me. The important point is that no one takes the suggestion seriously.

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if variable has something

02 Aug 2019, 05:39

- @Helgef: Your argument falls down when Sleep, 0 is used in a context where the 'caveats' are irrelevant.
- Now, since Sleep, 0 can do *more* than sleep for 0 milliseconds, that encourages looking for a better one-line NoOp/pseudo-NoOp ...
- Do you have any qualms about any of these 3:
DllCall("kernel32\Sleep", "UInt",0), SoundBeep,, 0 or (0).

- Have you considered fixing rather than repeating posts? (Partly to avoid a short topic stretching to multiple pages unnecessarily.)
- Are you being polite and measured? The important point is that if you often write disproportionate posts, no one is going to want to talk to you.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ntepa and 249 guests