Page 1 of 1

[SOLVED] Functions question...when to use double vs single quotes?

Posted: Wed Jun 24, 2020 4:15 pm
by crispyjones
I've always used double quotes when dealing with variables in functions. Like this function %my_usr_var% = $left($proper($alrep("%s_original_name%","")),28) to shorten and remove spaces etc from filenames in a populate variable node.

Ran into some issues with using the $jsonencode function to create a JSON friendly version of %s_error%. For the below examples assume "%s_error%" is equal to Youtube-dl@HOSTEN250-04: Could not download: "swarm shake 2019.mp4"

My initial attempt was this
%json_error% = $jsonencode("%s_error%") but the value of %json_error% became $jsonencode("Youtube-dl@HOSTEN250-04: Could not download: "swarm shake 2019.mp4"") which is not JSON friendly.

When I changed it to single quotes
%json_error% = $jsonencode('%s_error%') the value of %json_error% became Youtube-dl@HOSTEN250-04: Could not download: \"swarm shake 2019.mp4\" which is correct.

Looking for insight on when to use single ' quotes vs double " quotes. I want to go through my variable nodes and make sure they are correct.

Re: Functions question...when to use double vs single quotes?

Posted: Wed Jun 24, 2020 6:52 pm
by emcodem
Hey crispy,

very good question, this might give you some more insight:
https://www.autoitscript.com/forum/topi ... le-quotes/
Basically it says, use single quotes when your text contains double quotes and vice-versa.

If i am not wrong, you always use double quotes for filenames because they cannot contain one. But for larger text it gets a little tricky... @admin might tell us a little more about it and we should also mention this in the wiki

Re: Functions question...when to use double vs single quotes?

Posted: Thu Jun 25, 2020 7:19 am
by admin
Hi cripsyjones,

If your literal string does NOT contain neither double nor single quotes it really does not matter. For example:

"%s_my_variable" is just as good as '%s_my_variable'. On the other hand:

"my "string" is cool" and 'my 'string' is cool' will NOT work.
If you are usure or you string contains mixed quotes, you should store the string in a variables before using it in a function.

Hope this helps.

-steinar

Re: Functions question...when to use double vs single quotes?

Posted: Fri Jun 26, 2020 6:50 pm
by crispyjones
Because this is an error reporting block it's hard to guess what might be in the %s_error% string. I tested your solution for mixed quotes, I was skeptical, but it worked (at least on my error containing double quotes)! Thank you.
populateVar.jpg
populateVar.jpg (81.33 KiB) Viewed 4609 times