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

Questions and answers on how to get the most out of FFAStrans
Post Reply
crispyjones
Posts: 106
Joined: Wed Dec 27, 2017 3:21 am

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

Post 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.
Last edited by crispyjones on Fri Jun 26, 2020 6:51 pm, edited 1 time in total.
emcodem
Posts: 1811
Joined: Wed Sep 19, 2018 8:11 am

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

Post 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
emcodem, wrapping since 2009 you got the rhyme?
admin
Site Admin
Posts: 1687
Joined: Sat Feb 08, 2014 10:39 pm

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

Post 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
crispyjones
Posts: 106
Joined: Wed Dec 27, 2017 3:21 am

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

Post 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 5233 times
Post Reply