expressions within strings Topic is solved

Discuss the future of the AutoHotkey language
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

expressions within strings

22 Sep 2016, 10:32

i was playing around with a few examples today, to see if this new feature could replace some of those Eval() functions, and i had a few questions.


1. it looks like the sub-expression within the %%s is evaluated first. should the v2-changes doc make a mention of that? originally i wasn't sure if i needed to escape quotes or not

Code: Select all

; doesn't work when escaping quotes 
MsgBox("the floor of 10.1 is %Floor(`"10.1`")%")

; works when not escaping
MsgBox("the floor of 10.1 is %Floor("10.1")%")

; works using opposite quotemark
MsgBox("the floor of 10.1 is %Floor('10.1')%")

; works here, but might be confused with auto-concat and literal %s ?
MsgBox("the floor of 10.1 is %Floor(" 10.1 ")%")
similarly:

Code: Select all

var := 8

;this works
MsgBox("the floor of 10.1 is %Floor("%(var + 2.1)%")%")

;this doesnt
MsgBox("the floor of 10.1 is %Floor(%(var + 2.1)%)%")
so i'm confused on how it actually works



2. is there anything to worry about with the internal string/number types when doing something like this?

Code: Select all

var := 8
MsgBox(Floor("%var + 2.1%"))

3. i want this msgbox to display "10" but i can't seem to get this to work, no matter what i try. is it possible or am i trying to nest too deep? v2-changes says: "Strings and expressions can be nested to any depth."

Code: Select all

var := 8

; variable contains invalid char
flr := "Floor(var + 2.1)"
MsgBox(%flr%)

; variable contains invalid char
flr := "Floor('%var + 2.1%')"
MsgBox(%flr%)

; runs but outputs: Floor('10.09999999999999964')
flr := "Floor(%(var + 2.1)%)"
MsgBox("%flr%")

; runs but outputs: Floor('10.09999999999999964')
flr := "Floor('%var + 2.1%')"
MsgBox("%flr%")

; variable contains invalid char
flr := "Floor('%var + 2.1%')"
MsgBox("%(%flr%)%")
i'm spiraling into inception mode :crazy:

User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: expressions within strings

22 Sep 2016, 11:44

I think you're assuming you need %'s while in expression mode. You do not.

These examples all work.

Code: Select all

MsgBox % Floor(10.1)
MsgBox % "The floor of 10.1 is " . Floor(10.1)
var := 8
MsgBox % Floor(var + 2.1)
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

Re: expressions within strings

22 Sep 2016, 12:53

yes of course you're right, i was just playing around to test the limits of the new functionality

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: expressions within strings

22 Sep 2016, 20:12

guest3456 wrote:1. it looks like the sub-expression within the %%s is evaluated first.
First in what sequence? How else could it be evaluated?

MsgBox("the floor of 10.1 is %Floor("10.1")%") is the same as MsgBox("the floor of 10.1 is " . (Floor("10.1"))). How would the concatenation operation be evaluated without first evaluating each of its operands?
; doesn't work when escaping quotes
MsgBox("the floor of 10.1 is %Floor(`"10.1`")%")
It works the same as it would outside of percent signs.

Code: Select all

MsgBox % `"10.1`")"
There is no meaning in escaping a quote mark when it is not inside a quoted string, so the first escape character is ignored.
;this doesnt
MsgBox("the floor of 10.1 is %Floor(%(var + 2.1)%)%")
Floor(%(var + 2.1)%) contains an invalid double-deref, hence the message about the "variable name".
2. is there anything to worry about with the internal string/number types when doing something like this?
If you put it in quotes, it's a string. If you don't want a string, there's no reason to use quote marks, with or without percent signs inside them.
3. i want this msgbox to display "10" but i can't seem to get this to work, no matter what i try.
"%Expr%" is not Eval(). In the first place, %Expr% must be "Within a quoted string". MsgBox(%flr%) is not within a quoted string; it is "Elsewhere: Dynamically retrieves a variable by name".

Expr itself is the expression, not a variable containing a string representing an expression. % within a double-quoted string is effectively short-hand for " . ( or ) . ". It's not "functionality"; just "syntax".
originally i wasn't sure if i needed to escape quotes or not
The documentation says
Embeds an expression (Expr) within a quoted string, or dynamically retrieves a variable by name. Percent signs cannot be used directly within Expr due to ambiguity, but can be nested within quoted strings and parentheses. Otherwise, Expr can be any expression.
Quote marks which begin or end strings are not escaped when used in other expressions, so they are not escaped here.
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

Re: expressions within strings  Topic is solved

22 Sep 2016, 22:12

lexikos wrote: Expr itself is the expression, not a variable containing a string representing an expression. % within a double-quoted string is effectively short-hand for " . ( or ) . ". It's not "functionality"; just "syntax".
gotcha, that clears it up. i also hadn't seen that new doc either, so good to know


Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 26 guests