How I can use IF statement for empty variable ? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
xMaxrayx
Posts: 136
Joined: 06 Dec 2022, 02:56
Contact:

How I can use IF statement for empty variable ?

Post by xMaxrayx » 29 May 2023, 20:52

Hi, I used this code but it doesn't work.

Code: Select all

if (gg = "" ) 
                    {
                        MsgBox ("empty" )
                    }







this is my whole script.

Code: Select all

 b::
               {
    A_Clipboard := "" 
    gg := ""
    Send "^c"
    ClipWait
    gg := A_Clipboard
     


        
                    if (gg = "" ) 
                    {
                        MsgBox ("empty" )
                    }
                    
                    else if (gg = "]") 
                    {
                        MsgBox ("]")
                    } 
                    
                    else if (gg = "a") 
                        {
                            MsgBox ("a")
                        } 
                    
                    
                    else
                    {
                    MsgBox ("you pressed others keys")
                    }
                }




I have tested with "A" and "]" and it works with them however if the variable was empty it will use "else"
I know I can use clipwait mothed but I don't want to use it.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: How I can use IF statement for empty variable ?

Post by boiler » 29 May 2023, 21:47

It’s clearly not empty. What makes you think it is? In fact, it can’t ever be empty because you have the ClipWait waiting indefinitely until the clipboard contains something, so gg can never be assigned an empty clipboard.

User avatar
xMaxrayx
Posts: 136
Joined: 06 Dec 2022, 02:56
Contact:

Re: How I can use IF statement for empty variable ?

Post by xMaxrayx » 29 May 2023, 22:48

boiler wrote:
29 May 2023, 21:47
It’s clearly not empty. What makes you think it is? In fact, it can’t ever be empty because you have the ClipWait waiting indefinitely until the clipboard contains something, so gg can never be assigned an empty clipboard.
yeah you are correct.

I removed clipwait and it always gives me Else even if I have ] or a in my clipbored

Code: Select all

 b::
               {
    A_Clipboard := "" 
    gg := ""
    Send "^c"
    
    gg := A_Clipboard
     


        
                    if (gg = "" ) 
                    {
                        MsgBox ("empty" )
                    }
                    
                    else if (gg = "]") 
                    {
                        MsgBox ("]")
                    } 
                    
                    else if (gg = "a") 
                        {
                            MsgBox ("a")
                        } 
                    
                    
                    else
                    {
                    MsgBox ("you pressed others keys")
                    }
                }

I tried with sleep(50) but it can't detect empty clipboard :headwall:

Code: Select all

 b::
               {
    A_Clipboard := "" 
    gg := ""
    Send "^c"
    Sleep(100)
    gg := A_Clipboard
    


        
                    if (gg = "" ) 
                    {
                        MsgBox ("empty" )
                    }
                    
                    else if (gg = "]") 
                    {
                        MsgBox ("]")
                    } 
                    
                    else if (gg = "a") 
                        {
                            MsgBox ("a")
                        } 
                    
                    
                    else
                    {
                    MsgBox ("you pressed others keys")
                    }
                }

[Mod edit: Changed quote tags to code tags.]
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: How I can use IF statement for empty variable ?  Topic is solved

Post by boiler » 30 May 2023, 00:29

The clipboard clearly doesn’t contain the things you think it does. What are you copying from? An Excel cell or something that doesn’t contain only what you think it contains when you copy it?

xMaxrayx wrote: I know I can use clipwait mothed but I don't want to use it.
Why not? It’s the right thing to do. Unless you have a good reason not to (which I doubt), then use it.

Don’t use quote tags for your code. Always use code tags.

User avatar
xMaxrayx
Posts: 136
Joined: 06 Dec 2022, 02:56
Contact:

Re: How I can use IF statement for empty variable ?

Post by xMaxrayx » 30 May 2023, 02:29

boiler wrote:
30 May 2023, 00:29
The clipboard clearly doesn’t contain the things you think it does. What are you copying from? An Excel cell or something that doesn’t contain only what you think it contains when you copy it?

xMaxrayx wrote: I know I can use clipwait mothed but I don't want to use it.
Why not? It’s the right thing to do. Unless you have a good reason not to (which I doubt), then use it.

Don’t use quote tags for your code. Always use code tags.

idk I just put the text cursorin Notepad and pressed Ctrl+c but I found out in Windows clip-history it won't show anything empty so seems Windows doesn't copy empty thing.




Why not? It’s the right thing to do. Unless you have a good reason not to (which I doubt), then use it.




because it's easier to read, and re-edit compare to Clipwait mothed because everything is close to each other

this is code will work with clipwait mothed

Code: Select all

 b::
               {
    A_Clipboard := "" 
    
    Send "^c"
    if !ClipWait(0.1)
        {
            MsgBox "empty"
            return
        }
        gg := ""  
        gg := A_Clipboard

                    
                     if (gg = "]") 
                    {
                        MsgBox ("]")
                        return
                    } 
                    
                    else if (gg = "a") 
                        {
                            MsgBox ("a")
                            return
                        } 
                    else 
                        {
                          MsgBox ("you select other stuff") 
                          return
                        }    

                    
                    
                    
                }

I wish I can do like this but it won't work. but it will be more fun if I can do it

Code: Select all

 b::
               {
    A_Clipboard := "" 
        Send "^c"
        gg := ""  
        gg := A_Clipboard

                    if !ClipWait(0.1)
                        {
                            MsgBox "empty"
                            return
                        }
                     if (gg = "]") 
                    {
                        MsgBox ("]")
                        return
                    } 
                    
                    else if (gg = "a") 
                        {
                            MsgBox ("a")
                            return
                        } 
                    else 
                        {
                          MsgBox ("you select other stuff") 
                          return
                        }    

                    
                    
                    
                }


sorry for the quote tags seems was my other script running in the background and I forget to close it.
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: How I can use IF statement for empty variable ?

Post by boiler » 30 May 2023, 06:09

I don’t know what makes that more fun. It makes no sense having the ClipWait after you’ve assigned the clipboard contents to gg. You could be assigning them before the clipboard is loaded with the copied text, so gg might never contain what was actually copied.

I’m done here. I don’t understand why you have a version that works but you want to have more fun by trying to make nonsensical code work. Good luck.

Post Reply

Return to “Ask for Help (v2)”