Conditional Structures (if-then-else)

All about user-defined episode / movie format expressions
Post Reply
User avatar
rednoah
The Source
Posts: 13958
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Conditional Structures (if-then-else)

Post by rednoah » 03 Oct 2016, 18:53

Conditional Structures

You can use if-then-else blocks or the ternary operator for conditional code.

Example 1: Move 3D movies and normal movies into different folders:

Code: Select all

{fn =~ /3D/ ? '3D Movies' : 'Movies'}
Example 2: Add [10bit] to 10-bit videos but not to 8-bit videos:

Code: Select all

{if (bitdepth == 10) ' [10bit]'}
OR

Code: Select all

{bitdepth == 10 ? ' [10bit]' : null}
Example 3: Sort movies into folders by collection if the movie belongs to a collection or genre folders if not:

Code: Select all

{any{collection}{genre}{'No Genre'}}
The any(Closure...) function will call each of the given Closures until it finds one that yields a result.



Bindings and functions may throw exceptions so the result may be neither true nor false:

Code: Select all

{'Avatar'.match(/3D/) ? 'X' : 'Y'} ⇨ Exception: Pattern not found
:idea: Please read the FAQ and How to Request Help.

UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Conditional Structures (if-then-else)

Post by UKenGB » 07 Jan 2017, 18:17

Can either method be nested?

Any 'case' structures possible?

User avatar
rednoah
The Source
Posts: 13958
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Conditional Structures (if-then-else)

Post by rednoah » 07 Jan 2017, 18:44

1.
Yes, ternary expressions and all control structures in general can be nested, as one would expect from a general purpose programming language.

2.
Yes, but switch statements tend to be a bit too verbose for my taste for simple format expressions.
:idea: Please read the FAQ and How to Request Help.

hansooloo
Posts: 25
Joined: 06 Feb 2016, 14:58

Re: Conditional Structures (if-then-else)

Post by hansooloo » 25 Jul 2018, 22:46

How can I actually write an if-then-else?

e.g.,
IF movie is part of a collection
name it `collection/plex`
ELSE
name it `plex`

If I use this

Code: Select all

{if (collection != null) {collection/plex} else {plex}}
I get this

Code: Select all

ExpressionException: No signature of method: java.lang.String.div() is applicable for argument types: (File) values: [Movies/The Purge (2013)/The Purge (2013)]
Possible solutions: div(java.lang.String), is(java.lang.Object), wait(), trim(), size(), find()

User avatar
rednoah
The Source
Posts: 13958
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Conditional Structures (if-then-else)

Post by rednoah » 26 Jul 2018, 01:17

1.
If you just want to add the collection in front of the plex path, then you don't need if-then-else:

Code: Select all

{collection+'/'}{plex}
If you want a default value if collection is undefined, the I'd write something like this:

Code: Select all

{any{collection}{'No Collection'}/{plex}
You can't check if collection == null because of theThe Unwind-on-Undefined Behaviour, meaning collection is never null, it's either not null, or it crashes the {expression}.


2.
However, the error message you posted is completely unrelated to if-then-else and just tells us that we can't use the / operator between a String object and a File object.
:idea: Please read the FAQ and How to Request Help.

Post Reply

Who is online

Users browsing this forum: Google [Bot], Metrix84 and 0 guests