conversion logic, v1 = -> v1 := -> v2, two-way compatibility

Discuss the future of the AutoHotkey language
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 02:28

jeeswg wrote:Btw is there nothing better for
'if A then do B (else do nothing)' than:
(vWinClass = "Winamp v1.x") ? (vUtil := "Winamp") : ""
Thanks.
There is this traditional one-liner:

Code: Select all

IfEqual, vWinClass, Winamp v1.x, SetEnv, vUtil, Winamp
Maybe you would not consider that as being better.
User avatar
jeeswg
Posts: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 03:03

Yes, I never liked the way the command style looks but it's objectively very good code, much appreciated.

Normally, if it's one line, that's all the matters (because of hotstrings),
but it always looks kind of bulky.

There were always some sneaky things you could do by commands
but not by functions. I think you could do things with break/continue/Return,
can't remember!
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 03:39

I use these a lot, I'm not sure if you refer to these?

Code: Select all

; used inside subroutines
FileSelectFile, selected_File, 1, %Filename%, Select File
IfEqual, ErrorLevel, 1, Return ; user cancelled dialog

; used inside Loops
InputBox, Answer, %Title%, %Prompt%,, %Width%, %Height%,,,,, %Default%
IfEqual, ErrorLevel, 1, Break ; user cancelled InputBox

; also used inside Loops
IfEqual, A_LoopField,, Continue ; ignore empty lines
HotKeyIt
Posts: 1866
Joined: 29 Sep 2013, 18:35
Contact:

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 04:31

jeeswg wrote:Btw is there nothing better for
'if A then do B (else do nothing)' than:
(vWinClass = "Winamp v1.x") ? (vUtil := "Winamp") : ""
Thanks.
In v2 there is

Code: Select all

vWinClass := "Winamp v1.x"
If vWinClass = "Winamp v1.x", vUtil := "Winamp"
MsgBox % vUtil
guest3456
Posts: 2553
Joined: 09 Oct 2013, 10:31

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 08:42

^another reason to JUST USE V2

your time and efforts are better spent helping others learn the differences in v2, teaching them the benefits of expression syntax, and writing docs to facilitate all of that

i've said it many times, all of these compatibility issues can be solved with some good pages explaining the transition. it just needs to get started and then we can iterate and improve it. but of course, who doesn't hate writing docs? everyone hates it

User avatar
jeeswg
Posts: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 12:17

@wolf_II: Wow, bringing back memories, nicely done.
Since you can't do assignment on IfEqual,
no one method can do everything!

===============

@HotKeyIt: This is nice, I always wanted something like this,
when I started using AutoHotkey, I already knew Excel VBA.
I always thought AutoHotkey could be: 'like Excel VBA plus curly brackets'
... I think that's the perfect programming language.

I was thinking why not have a command style like this, with no commas:
MyCommand "Param1" "Param2" "Param3" "Param4"
MyCommandAsFunc(Param1, , Param3), when you don't need all the parameters.
Just scrap commas altogether for the command style.
Excel has one-liner 'If A Then B' and functions like InStrRev and Rept (aka String)
amongst others that I thought would be good as default commands for AHK,
since the beginning.
[EDIT: Actually, without commas you *must* put in . to concatenate strings, so keep commas! That's an advantage over Excel VBA that needs constant ampersands, &, to concatenate.]

===============

@guest3456: Haha I don't hate writing docs, just that I'm doing so many
AHK things at the same time, should I even be spending this much time on AHK!
But I decided that for a few months, I would spend more time on it than usual,
to finish a great many things.
I look forward to collecting my notes on AHK v2, I can churn out some good docs
pretty fast, maybe a few weeks.

The problem with AHK v2 take-up is that most code at the moment is still written in AHK v1.
To improve AHK v2 take-up, I think we have to be creating users fluent in both AHK v1 and AHK v2
right now, who know how to convert their scripts, and some like me are seeking
two-way compatible scripts initially, as far as possible, and why wouldn't anybody want that convenience.

I've very recently come to the conclusion that putting more features into AHK v1 to ease the transition,
while keeping AHK v2 clean and pure, is probably the best way to go, with some possible AHK v1 mode
global variables for AHK v2.

Re. tutorials/docs, I've started with a character guide, because characters are the hardest thing to search for in any documentation.
jeeswg's tutorial pages - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=26486
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: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 14:06

Btw one thing that would help AutoHotkey, newbies and experts alike,
would be a #Warn mode in AHK v1 that discourages some script practices
in favour of others, that would then lead to your scripts and coding practices
being far more ready for AHK v2 than otherwise.
I know that I would have quite liked this a while go, when I started
to become less of a beginner and more intermediate.
E.g. use expression style rather than command style for variable manipulation and if statements.

Btw I don't like #Warn as it stands, because having local/global variables,
with the same name is absolutely fine, I don't want to be warned about this.
I don't know what else it warns about ... because I couldn't get past those warnings.
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
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

22 Jan 2017, 15:14

You definitively wouldn't want a Warn mode like that because it would encourage you to stop writing your Spaghetti Code.
Recommends AHK Studio
lexikos
Posts: 6483
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

23 Jan 2017, 01:59

jeeswg wrote:Btw is there nothing better for
'if A then do B (else do nothing)' than:
(vWinClass = "Winamp v1.x") ? (vUtil := "Winamp") : ""
"Better" is very subjective. I probably wouldn't write it as one line in the first place.

If you want an alternative,

Code: Select all

(vWinClass = "Winamp v1.x") && (vUtil := "Winamp")
User avatar
jeeswg
Posts: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

23 Jan 2017, 02:25

Wow, I thought maybe you might have another idea!
That there was another method hiding somewhere in plain sight!
Better than my Christmas presents I can tell you that much, CHEERS.

Btw people, I don't deliberately try to be obscure!
I would use this one generally because it's clearer:
(vWinClass = "Winamp v1.x") ? (vUtil := "Winamp") : ""
However if I had a long list of if/then statements, I might use the new method.

Every action has an equal and opposite reaction.
(vWinClass != "Winamp v1.x") && (vUtil := "not Winamp")
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: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 01:29

I've produced this to help with script conversion:
list of every command/function/variable from across all versions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=27321

Btw: So what other tricks are there in AutoHotkey?
Other than commands with secret parameters.

[Btw 2: my 300th post, - this is Sparta! -]

[EDIT:]
At the moment I'm trying to fully convert my scripts, it's going pretty well.
I noticed in AHK v2 you can do `" instead of "", and I think this would be
really useful for AHK v1. It's the first conversion-related change to AHK v1,
that I feel 99% certain would be useful.
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: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 16:40

OK this is a new problem: you cannot use "" for a literal double quote in an expression in AHK v2.

Best workaround?

Code: Select all

;vText := "a""b" ;not allowed in AHK v2
;vText := "a`"b" ;not allowed in AHK v1
global vDQ := Chr(34) ;at the start of the script
vText := "a" vDQ "b" ;workaround
vText := "a" Chr(34) "b" ;workaround
If you could do `" instead of/as well as "", in AHK v1, this would be
really useful for AHK v1 generally, regardless of converting scripts,
subject to implementation difficulty.

#EscapeChar (and explanation of escape sequences)
https://autohotkey.com/docs/commands/_EscapeChar.htm\
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 16:45

Try MsgBox, % "a" '"' "b".
Or MsgBox, % 'a"b'
User avatar
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 17:15

I told you to analyze the syntax completely and then convert the parts you need.
I even gave you an approach at how to make things less complicated and make things modular.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 17:33

Apologies, maybe I should have been clearer,
I'm looking for a two-way compatible method.
That's the plan behind this conversion project.
All the problems I've faced up till now,
have had nice solutions.
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
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 17:43

And that's also easily possible.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 18:13

Code: Select all

;my current best solution, use overscore as a character in place of double quote:
;(note: in AHK v1, this does the replacement, and a deref, here in AHK v2 only a replacement:)
;in both versions the function would be called DerefX or something else
;q:: ;command style in expressions two-way compatible (AHK v2 version example):
vPathExe := "C:\Windows\System32\notepad.exe"
vPath := "C:\hello.txt"
vTarget := DerefX2("¯%vPathExe%¯ ¯%vPath%¯")
MsgBox % vTarget
Return

DerefX2(vText)
{
Return StrReplace(vText, Chr(175), Chr(34), , -1)
}
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
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 18:43

Your current best solution is extremly flawed.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

25 Jan 2017, 18:59

Some pseudocode/approach details or code would be great, cheers.
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: 6644
Joined: 19 Dec 2016, 01:58
Location: UK

Re: conversion logic, v1 = -> v1 := -> v2, two-way compatibility

27 Jan 2017, 01:19

Conversion is actually going really well, pretty soon I'll have all my scripts AHK v1/AHK v2 two-way compatible.
I have essentially completed conversion on one of the subscripts included in my main scripts.

The big problem at the moment is that there is a not common way of handling double quotes.
Either "" or `", and I'm tempted to leave certain conversions undone, until (if) there is a way of doing this.
My hoped for option would be to bring `" into AHK v1, because "" was never satisfactory for AHK v1 in the first place.

One of the main issues is converting command style to expression style.
Basically I think of it like this:

text%variable%text%variable%text
%variable%text%variable%

Imagine the latter as the same as the former,
but with blank text either side.
Thus if you parse this, with % as the delimiter, every odd item is text,
and every even item is a variable.

This is my function at present, written for AHK v1:

Code: Select all

;==================================================

JEE_StrCmdStyleToXpnStyle(vText, vOpt="")
{
if (SubStr(vText, 1, 2) = "`% ")
Return SubStr(vText, 3)
if (vText = "")
Return """"""

vText := JEE_StrReplace(vText, "`%A_Space`%", " ", "All")
vText := JEE_StrReplace(vText, "`%A_Tab`%", "``t", "All")

vUnused := JEE_StrGetUnusedChar1Var(vText)
vText := JEE_StrReplace(vText, "```%", vUnused, "All")

vOutput := ""
Loop, Parse, vText, `%
{
vTemp := A_LoopField
if (A_Index & 1)
	{
	if (vTemp = "")
	continue
	vTemp := JEE_StrReplace(vTemp, """", """""", "All")
	vOutput .= """" vTemp """ "
	}
else
	{
	vOutput .= vTemp " "
	}
}

vOutput := JEE_StrReplace(vOutput, vUnused, "```%", "All")
vOutput := SubStr(vOutput, 1, -1)

if (vOutput = "")
Return """"""
if !InStr(vOpt, "x")
if (SubStr(vOutput, 1, 1) = """") AND (SubStr(vOutput, 1-1) = """")
if JEE_StrIsDigit(SubStr(vOutput, 2, -1))
Return SubStr(vOutput, 2, -1)
Return vOutput
}

;==================================================

JEE_StrReplace(ByRef vInputVar, vSearchText, vReplaceText="", vAll=0, vCaseSen=0)
{
vSCS := A_StringCaseSense
StringCaseSense, % vCaseSen ? "On" : "Off"
StringReplace, vOutput, vInputVar, % vSearchText, % vReplaceText, % vAll
StringCaseSense, % vSCS
Return vOutput
}

;==================================================

JEE_StrGetUnusedChar1Var(ByRef vText, vDelim="", vExclude="")
{
if !(vDelim = "")
if !InStr(vExclude, vDelim)
if !InStr(vText, vDelim)
Return vDelim

Loop, 255
{
vDelim := Chr(A_Index)
if !InStr(vExclude, vDelim)
if !InStr(vText, vDelim)
Return vDelim
}

;JEE_MsgBox("JEE_StrGetUnusedChar1Var:`r`nerror: no available delimiter found")
MsgBox % "JEE_StrGetUnusedChar1Var:`r`nerror: no available delimiter found"
Return ""
}

;==================================================

JEE_StrIsDigit(vText, vOpt="")
{
if (vOpt = "x") AND (vText = "")
Return 0

if vText is digit
Return 1
else
Return 0
}

;==================================================
I have the 17 original issues, plus the 18th issue of double quotes,
any more and it'll be my - 19th Nervous Breakdown -.
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 “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 12 guests