Page 1 of 1

Need help understand if/else,

Posted: 20 Jul 2016, 11:56
by Crankrune
I've been using FileBot for while, using Google and these forums to figure out how to do what I've wanted to do. And so far that's worked most of the time. Except with if/else statements. I have a basic understanding of them, but haven't really figured out how they work. The only thing I've really figured out with them is this basic way of adding the video quality if it's 720p or 1080p.

Code: Select all

{if (vf=="720p") " [720p]" else ""}
But I have not idea how to use it when something is a variable. For example I'm trying to use it (if/else) to write "Special" if something is a special, and "Episode" if it's not. However the "{special}" outputs the special number, which would be different for each special, meaning without writing a thousand lines, doesn't work.

Similarly I've seen what looks to be another kind of if/else statement written with "?" & ":", but even less sure of how those work. One last thing, is it possible to have the output of the if/else be a variable e.g. "{s}" or "{e}"?

Please and thanks for any help.

Re: Need help understand if/else,

Posted: 20 Jul 2016, 12:26
by rednoah
The regular binding will give you a boolean value:

Code: Select all

{regular ? '' : ' [Special]'}
Since special will unwind-on-undefined, this will work as well:

Code: Select all

{special; ' [Special]'}
The outer {...} define an entire expression, not just a variable:

Code: Select all

{regular ? 'E'+e : 'SP'+special}
@see viewtopic.php?f=5&t=1895

Re: Need help understand if/else,

Posted: 20 Jul 2016, 13:11
by Crankrune
Thanks for the help. Here's my whole expression.

Code: Select all

{file.path[0]}:\TV\{csv('H:/TV/Names.csv').get(n.replaceTrailingBrackets(replacement = "").replace(':', " -")) ?: n.replaceTrailingBrackets(replacement = "").replace(':', " -")}\{episode.special ? 'Specials/' : ''}{'Season '+s}{' '+[vf.match(/720[pP]|1080[pP]/)]}\{episode.special ? 'Special '+special.pad(2) : 'Episode '+e.pad(2)} - {t.replacePart(replacement = ' [Part $1]').replace("`","'").replace("?", "!").replace('"', "''").replace(':', " -")}{' '+[vc.match(/HEVC|x265/)]}
I do have a question about this, the part that adds the quality. I copied it from someone else in the 'format expressions' thread, and am not entirely sure how a couple parts of it work.

1. The forward slashes instead of quotations, why? Does it work differently or are they interchangeable?

2. The 'p' in brackets, what does that do?

Again, please and thanks for the help! FileBot is amazing!

Re: Need help understand if/else,

Posted: 20 Jul 2016, 15:43
by rednoah
1.
It's the same. /.../ is usually used for regular expressions because escaping ', " and \ is annoying.

There is many ways to define a String value:
http://groovy-lang.org/syntax.html#all-strings


2.
[pP] is a regular expression:
http://www.regular-expressions.info/quickstart.html

It means "p or P" but in this case it doesn't do anything because String.match(pattern) is case-insensitive by default.

Re: Need help understand if/else,

Posted: 23 Jul 2016, 22:49
by charlesellzy
I have learnt if-else statement in c, cpp, and java. This is a new to me indeed.