Using [ and ( in variables for comparison

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MorkB
Posts: 3
Joined: 05 Dec 2022, 11:06

Using [ and ( in variables for comparison

Post by MorkB » 05 Dec 2022, 11:14

I followed the AutoHotkeys tutorial and searched a lot online but can't find anything so here goes!
I have to do a lot of changing the brackets around some text for example this: [aString] -> (aString)
or vice versa
I have managed to do the basic task, but I am having trouble with some logic.
I want to use the same command to do both things. I use:

Code: Select all

SubGet(LongString, 1)
to access the first char in the string; either '[' or '('
I want to then compare in an if statement like this:

Code: Select all

FirstChar := StrGet(Current_Clipboard, 1)
SquareBracket := [
RoundBracket := (
if (FirstChar = SquareBracket)
{ 
	StringReplace, Current_Clipboard, Current_Clipboard, [, (
	StringReplace, Current_Clipboard, Current_Clipboard, ], )	
}
else if(FirstChar = RoundBracket )
{
	StringReplace, Current_Clipboard, Current_Clipboard, (, ]
	StringReplace, Current_Clipboard, Current_Clipboard, (, ]
}
But there is a problem with assigning

Code: Select all

SquareBracket := [
.
I have tried many unsuccesful attempts to put %[% or '[' etc but I am grasping at straws.

How do I do this? Or how to I work the logic in a better way than this

Thanks

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Using [ and ( in variables for comparison

Post by Chunjee » 05 Dec 2022, 12:38

https://biga-ahk.github.io/biga.ahk/#/?id=head (also accessible as .first) will return the first character in a string

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

A.first("fred")
; => "f"

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Using [ and ( in variables for comparison

Post by Chunjee » 05 Dec 2022, 12:40

I think

Code: Select all

SquareBracket := "["
RoundBracket := "("
would also help your code.


Current_Clipboard is not defined.

You have some functions I don't have. To plainly get the first character with ahk; please read https://www.autohotkey.com/docs/commands/SubStr.htm

MorkB
Posts: 3
Joined: 05 Dec 2022, 11:06

Re: Using [ and ( in variables for comparison

Post by MorkB » 07 Dec 2022, 09:07

Sorry I didnt post my full code. Here it is:

Code: Select all

 
 ; Replace [] with () around selected text
^b::
Old_Clipboard := Clipboard

Sendinput, ^{c} ; copy the selected text/item/wahtever
clipwait, 1,0
Current_Clipboard := Clipboard

FirstChar := StrGet(Current_Clipboard, 1)
SquareBracket := [
RoundBracket := (
if (FirstChar = SquareBracket)
{ 
	StringReplace, Current_Clipboard, Current_Clipboard, [, (
	StringReplace, Current_Clipboard, Current_Clipboard, ], )	
}
else if(FirstChar = RoundBracket )
{
	StringReplace, Current_Clipboard, Current_Clipboard, (, ]
	StringReplace, Current_Clipboard, Current_Clipboard, (, ]
}

Send %Current_Clipboard%
Clipboard := Old_Clipboard

return
If I change to := "[" then the if statement never returns true and so the brackets never get replaced

ThePeter
Posts: 49
Joined: 25 Oct 2022, 05:57

Re: Using [ and ( in variables for comparison

Post by ThePeter » 07 Dec 2022, 10:03

The literal brackets need to be in quotes. Your if statement does not return true because FirstChar will not contain the first character in your string. StrGet gets a string from a memory address. What you want to use is SubStr.

Code: Select all

 ; Replace [] with () around selected text
^b::
Old_Clipboard := Clipboard

Sendinput, ^{c} ; copy the selected text/item/wahtever
clipwait, 1,0
Current_Clipboard := Clipboard

FirstChar := SubStr(Current_Clipboard, 1, 1)
SquareBracket := "["
RoundBracket := "("
if (FirstChar = SquareBracket)
{ 
	StringReplace, Current_Clipboard, Current_Clipboard, [, (
	StringReplace, Current_Clipboard, Current_Clipboard, ], )	
}
else if(FirstChar = RoundBracket )
{
	StringReplace, Current_Clipboard, Current_Clipboard, (, ]
	StringReplace, Current_Clipboard, Current_Clipboard, (, ]
}

Send %Current_Clipboard%
Clipboard := Old_Clipboard

return
EDIT: For StringReplace, brackets are not used because this command does not use expression syntax. You may want to consider the more modern StrReplace instead, in which expressions are used. But both do the same really.

MorkB
Posts: 3
Joined: 05 Dec 2022, 11:06

Re: Using [ and ( in variables for comparison

Post by MorkB » 08 Dec 2022, 05:22

Thanks Chunjee and ThePeter, the quotation marks and SubStr were the problem!

Post Reply

Return to “Ask for Help (v1)”