Scripting - varible in caption

Things you’d like to miss in the future...
Forum rules
When reporting a bug, please include the following information: your XYplorer version (e.g., v27.90.0047), your Windows version (e.g., Win 11), and your screen scaling percentage (e.g., 125%). We recommend adding your Windows version and screen scaling percentage to your profile or signature. This will make debugging much easier for us.
Post Reply
binocular222
Posts: 1419
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Scripting - varible in caption

Post by binocular222 »

These scripts suppose to hide Menu B.
This works:

Code: Select all

"_Initialize"
	perm $Hide;
	$Hide = _Menu B
"Menu A" msg a
"$Hide" msg b
"Menu C" msg c
But this doesn't

Code: Select all

"_Initialize"
	perm $Hide;
	$Hide = _Menu B
"Menu A" msg a
"$Hide|<xyicons>\USB.ico" msg b
"Menu C" msg c
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

admin
Site Admin
Posts: 64868
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting - varible in caption

Post by admin »

Confirmed and fixed.

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Scripting - varible in caption

Post by PeterH »

admin wrote:Confirmed and fixed.
Don,
maybe you should define a page with some info about quoting, and when seeing illegal string definitions just give a reference to that page?
(Especially having seen the problems after deleting the depracted script function calls...)

@binocular222
You know that syntax rules say that strings have to be (single or double) quoted? So the way you wrote your script only functions by chance and might stop working tomorrow? The line in question would have to be

Code: Select all

   $Hide = '_Menu B'
(No variable inside the string, so you may use single or double quotes.)

For consistency I would also add a ; at the end, and I would combine it with the perm command to read:

Code: Select all

   perm $Hide = "_Menu B";
but this is only a matter of habits.

binocular222
Posts: 1419
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Scripting - varible in caption

Post by binocular222 »

XYplorer quoting rule is too much complicated for me.
In AHK language:
- All-and-only strings are quoted. Everything between quotes are simply string
- Variable; function... are not quoted
- Special characters are escaped by apostrophe (i.e: `% `n `$ `")

In XY:
- String can be either single or double quote but not always the same (see Example 1 below)
- Variables are resolved in double quote or no quote
- Sometime 2-double quotes are required to recognize file path with space (i.e: ::run ""C:\a b.exe"")
- There are many inconsistency in XY rule,
Example 1:
This works: ::echo replace("quote"is"removed",'"')
This not work: ::echo replace('quote"is"removed','"') //single quote not equal double quote
This not work: ::echo replace("quote"is"removed",""") //""" not equal '"'

Example 2:
::$Var = is; msg "This "$Var" good" //resolve in "This "is" good" - Why the outer-most quote appear?
::$Var = is; msg "This ""$Var"" good" //resolve in This "is" good - Not understand which is open quote and which is close quote
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Scripting - varible in caption

Post by PeterH »

Sorry: much to say...

But, if I understand it correct: you might do it like AHK?
Use *single* quotes, put all variables outside quotes, but don't forget to use concatenation sign! (A dot, *can* be surrounded by blanks.)
A varible name inside single quotes will be used as is:

Code: Select all

   $a = 'A string.';
   msg '$a = "' . $a . '"';
You are allowed to use resolved variable inside *double* quotes - but then you can't use the unresolved name as in my example above. So you have to modify:

Code: Select all

   $a = 'A string.';
   msg "a = '$a'";
Single quotes inside single quotes must be doubled, same for double inside double - that's why I used double inside single, or single inside double in examples above. Also correct would be

Code: Select all

   $a = 'A string.';
   msg "a = ""$a""";
For sure this can be complex for commands/functions that themself need quoted operands - then you have multiple levels of quoting, and combined with the rules for doubling quotes you might have to think about it for some time...
(...or test in debug mode)

And I think you make one mistake: you look what works. But this is just a temporary indication! You say: This works: ::echo replace("quote"is"removed",'"')
But it's illegal! If the first operand should be a string it had to be '"quote"is"removed"' ! (Note the additional single quotes!)
And as it's illegal you cannot compare the result it happens to show with something else: it's only by chance!
Or did you mean the two strings "quote" and "removed", concatenated by the unknown command is :?:

Your 2nd example echo replace('quote"is"removed','"') is correct: the string is quote"is"removed - and the functions removes the doulbe quotes from the string: OK!

The 3rd is 2 times wrong: echo replace("quote"is"removed",""")
- the 1st string might be quote"is"removed inside double quotes - but double quotes inside double quotes must be doubled!
- 2nd operand: the string is " inside double quotes: must be doubled!
So correct could be: echo replace("quote""is""removed","""")
or echo replace('quote"is"removed','"')
Or did you mean echo replace('"quote"is"removed"',"""")
or echo replace("""quote""is""removed""",'"')
(above: "or did you mean" says: I have to guess. A script parser cannot guess!)

I hope I wrote this in an understandable way?

admin
Site Admin
Posts: 64868
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting - varible in caption

Post by admin »

Why so many words? This small section from the Help file describes it completely:
Using Quotes in Scripting

It's strongly recommended that you (double- or single-) quote your strings! While the script engine is currently still permissive with unquoted strings (msg Hi! works) this might not be so in the future, so you better do msg "Hi!" right away!

(1) Everything is either in double-quotes, or in single-quotes, or outside of any quotes.
(2) To have double-quotes inside double-quotes they must be doubled.
(3) To have single-quotes inside single-quotes they must be doubled.
(4) Variables are resolved in double-quotes and outside of any quotes, but not in single-quotes.
And it is nothing exotic. Many wel-known languages, e.g. PHP, work exactly the same.

binocular222
Posts: 1419
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Scripting - varible in caption

Post by binocular222 »

Got it.
This makes me love AHK more. I've never made wrong quoting in AHK.
Double quote for string; no quote for the rest. No double-triple quotes, no single quote, no single-doble quote. Quite simple.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Filehero
Posts: 2713
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: Scripting - varible in caption

Post by Filehero »

binocular222 wrote:Double quote for string
What if the string contains a double quote literal?

Sorry for this dumb question but I don't know AHK.


Cheers,
Filehero

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Scripting - varible in caption

Post by TheQwerty »

binocular222 wrote:This makes me love AHK more. I've never made wrong quoting in AHK.
Double quote for string; no quote for the rest. No double-triple quotes, no single quote, no single-doble quote. Quite simple.
I'm sorry but I have to rain on this parade.. just like XY, AHK CAN be that "simple", but it does not HAVE to be.

Just look: http://www.autohotkey.com/docs/Variable ... te]Storing values in variables: To store a string or number in a variable, there are two methods: traditional and expression. The traditional method uses the equal sign operator (=) to assign unquoted literal strings or variables enclosed in percent signs. For example:

MyNumber = 123
MyString = This is a literal string.
CopyOfVar = %Var% ; With the = operator, percent signs are required to retrieve a variable's contents.
By contrast, the expression method uses the colon-equal operator (:=) to store numbers, quoted strings, and other types of expressions. The following examples are functionally identical to the previous ones:

MyNumber := 123
MyString := "This is a literal string."
CopyOfVar := Var ; Unlike its counterpart in the previous section, percent signs are not used with the := operator.[/quote]Strings are sometimes quoted but not always.
Retrieving the contents of variables: Like the two methods of storing values, there are also two methods for retrieving them: traditional and expression. The traditional method requires that each variable name be enclosed in percent signs to retrieve its contents. For example:
MsgBox The value in the variable named Var is %Var%.
CopyOfVar = %Var%
By contrast, the expression method omits the percent signs around variable names, but encloses literal strings in quotes. Thus, the following are the expression equivalents of the previous examples:

MsgBox % "The value in the variable named Var is " . Var . "." ; A period is used to concatenate (join) two strings.
CopyOfVar := Var
Variables are sometimes enclosed in percents and sometimes not.
if (CurrentSetting > 100 or FoundColor <> "Blue")
MsgBox The setting is too high or the wrong color is present.
In the example above, "Blue" appears in quotes because it is a literal string. To include an actual quote-character inside a literal string, specify two consecutive quotes as shown twice in this example: "She said, ""An apple a day."""
Oh there's double-quotes too!

The common parlance for this usage is to "escape" the significant characters, but try searching that page for "escape" when you don't remember whether AHK wants doubled-quotes, slashed-quotes, or backticked-quotes. Fortunately, they do explain escaping here but no mention of strings or quotes.


The biggest cause for confusion in XY is quoting the application and arguments in a Run/Open command. Unfortunately, this is a sore point in AHK as well: http://www.autohotkey.com/docs/commands ... m[quote]To pass parameters, add them immediately after the program or document name. If a parameter contains spaces, it is safest to enclose it in double quotes (even though it may work without them in some cases).[/quote]
When running a program via Comspec (cmd.exe) -- perhaps because you need to redirect the program's input or output -- if the path or name of the executable contains spaces, the entire string should be enclosed in an outer pair of quotes. In the following example, the outer quotes are shown in red and all the inner quotes are shown in black:
Run %comspec% /c ""C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt""
Ooff! I love AHK as well, though I'm not using it as much these days, but I'm not so sure it's a better example of handling these intricacies. ;)


If there is anything to take away from this it's that designing programming languages is hard!
Last edited by TheQwerty on 09 May 2013 18:27, edited 1 time in total.

binocular222
Posts: 1419
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Scripting - varible in caption

Post by binocular222 »

Literal " is escaped by prefixing " instead of apostrophe:

Code: Select all

Test := "double quote "" is escaped by prefixing double quote"
msgbox %Test%
Result:
double quote " is escaped by prefixing double quote
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Filehero
Posts: 2713
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: Scripting - varible in caption

Post by Filehero »

binocular222 wrote:Literal " is escaped by prefixing " instead of apostrophe:

Code: Select all

Test := "double quote "" is escaped by prefixing double quote"
msgbox %Test%
Result:
double quote " is escaped by prefixing double quote
Thanks.
Hmm, I'm still struggling to see the real/basic difference to the XY/PHP notation. :?

It doesn't matter what specific notation gets used, the basic challenge common to ALL programming/script languages doesn't go away: some char sequences have a special meaning and must be escaped to make them interpreted literally. Wether the escape signature char is """, "\" or "$anychar" doesn't matter in the end - you see, even within this post I had been forced to decide for one quotation char. :wink:


Cheers,
Filehero

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Scripting - varible in caption

Post by PeterH »

People are good in adapting.

The problem: if you adapted to one syntax, and then have to use another, it's always hard: you just see the disadvantages of the new, while you are adapted to those of the old syntax.

The result: it doesn't make sense to compare a syntax you are used to with a new one. You can't be fair.

The solution: try to understand the 'current' syntax, and act as appropriate...

Post Reply