Page 1 of 2
[SOLVED] Scripting Command Run & Variabless
Posted: 18 Mar 2013 02:07
by Enternal
I noticed that:
run "<xypath>\Rar.exe" a Data Data;
works fine but if I use:
run '"<xypath>\Rar.exe" a Data Data';
as shown in a similar example in the Help, XYplorer would not be able to find Rar.exe.
It became quite clear when I was initially using 7za.exe and needed it to zip the Data folder to zip format with:
run "<xypath>\7za.exe" a -tzip Data Data;
and it would not work yet I cannot add ' either since it would not be able to find 7za.exe.
It seems that if the variable is nested between " and ', it the variables will become literal so instead of <xypath> = C:\Users\SomePath, it will become exactly <xypath>.
Re: Scripting Command Run & Variabless
Posted: 18 Mar 2013 07:22
by highend
run """<xypath>\Rar.exe"" a Data Data";
Re: Scripting Command Run & Variabless
Posted: 18 Mar 2013 08:35
by Enternal
What?!?! How did that work? Why does that work? That's very interesting! Thank you!
Re: Scripting Command Run & Variabless
Posted: 18 Mar 2013 17:53
by PeterH
Enternal wrote:What?!?! How did that work? Why does that work? That's very interesting! Thank you!
In your 2nd example
run '"<xypath>\Rar.exe" a Data Data';
the whole parameter string
"<xypath>\Rar.exe" a Data Data
is single-quoted, i.e. taken as it is. And so
<xypath> is not substituted by it's value.
Re: Scripting Command Run & Variabless
Posted: 18 Mar 2013 20:13
by admin
Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 02:52
by Enternal
Thank you! I learned something new! So the single quote pretty much makes it take what inside of it as it is.
Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 03:30
by j_c_hallgren
Enternal wrote:Thank you! I learned something new! So the single quote pretty much makes it take what inside of it as it is.
Even though I'm not a script writer, I've seen literally dozens of posts here that were related to use or misuse of quotes and/or dbl quotes...it's probably one of the more important things to learn about XY scripts, it seems.
Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 11:45
by PeterH
One problem is the different meaning of single and double quotes.
But what should I say: it makes sense this way. But you must understand it
Another problem is the ability to often define strings *without* using quotes. If it works people often tend to learn "it's allowed" - and don't understand when in some cases it doesn't work. So please always avoid specifying strings without quotes!
(This is getting more problematic as there are still examples in help where quotes are missing

)
(Though I understand it's hard to correct them

)
Generally it would be helpful if XY would at least give some info/warning if a script omits quotes when neccessary - but I think the parser (or how you call it) isn't made for this.
And very best: always try to code as good as possible

Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 12:53
by admin
PeterH wrote:One problem is the different meaning of single and double quotes.
Note that I did not invent this. Like many other feeatures of XY scripting it's taken from PHP syntax.
Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 19:01
by PeterH
admin wrote:PeterH wrote:One problem is the different meaning of single and double quotes.
Note that I did not invent this. Like many other feeatures of XY scripting it's taken from PHP syntax.
I remember that when you explained the different usage of single and double quotes the first time, some years ago, I said I don't like it.
One reason: I was used that both are identical.
(And I was used to see syntax errors when a string was not quoted

)
Now I have used the different meaning of quotes for some time, and it's right: it has some advantages: as variables are allowed in double quoted strings you save a lot of starting/ending quotes and concatenation. Quite useful!
But if I need double quotes as characters, I must include them in single quotes - and lose the ability to include variables.
Or must use double quoted strings and double the included double quotes. (What a fine sentence

)
OK: each system has advantages and disadvantages
Now saying I've learned to live with the way it works: I had to learn that! And always keep it in mind. While some people doing scripting just want to quickly achive some result, but not look for such silly differences. I'm afraid for those it's not the best solution that they must look for quoting, and even differentiate between single and double quotes.
Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 19:09
by highend
But if I need double quotes as characters, I must include them in single quotes - and lose the ability to include variables.
Or must use double quoted strings and double the included double quotes. (What a fine sentence )
Or:
Code: Select all
$qm = chr(34);
$item = "<curitem>";
text "$qm$item$qm";
Ofc this is only useful, if you work with variables...
Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 20:11
by admin
There is also a function quote().
Re: Scripting Command Run & Variabless
Posted: 19 Mar 2013 23:41
by PeterH
highend wrote:But if I need double quotes as characters, I must include them in single quotes - and lose the ability to include variables.
Or must use double quoted strings and double the included double quotes. (What a fine sentence )
Or:
Code: Select all
$qm = chr(34);
$item = "<curitem>";
text "$qm$item$qm";
Ofc this is only useful, if you work with variables...
Please see what I say now just as your example: as further examples. Doesn't really say what's good or bad or so...
In the following code each line in a group is identical. (I hope...)
Code: Select all
$qm = chr(34);
$qm = """";
$qm = '"';
$item = "<curitem>";
$item = <curitem>;
text "$qm$item$qm";
text '"'.$item.'"';
text """$item""";
(The very last line, and a bit the 2nd are just what you wanted *not* to show :-) )
And sure: there is quote(). Not bad in this situation - but bad if this quoted expression is only part of the result: a function cannot be inside a quoted string, and as such has to be concatenated:
Code: Select all
text "item = ".quote($item).".";
text "item = ""$item""."
But as I mostly use double quoted strings I decided to generally use single quotes to qualify texts:
(Double quotes are more eye-catching - but here I prefer the simpler coding :-) )
Re: Scripting Command Run & Variabless
Posted: 20 Mar 2013 00:00
by Enternal
Lol! Looks like my bug report on quotes (actually due to my misuse and understanding of the double vs single quotes) have led to quite an interesting conversation about quotes! And... wow am I lost. I'm so used to single quotes and double quotes being the same as well and it turns out quite different than I have expected.
Re: Scripting Command Run & Variabless
Posted: 20 Mar 2013 14:29
by PeterH
Enternal wrote:Lol! Looks like my bug report on quotes (actually due to my misuse and understanding of the double vs single quotes) have led to quite an interesting conversation about quotes! And... wow am I lost. I'm so used to single quotes and double quotes being the same as well and it turns out quite different than I have expected.
Fine: then it was worth the words
