Conditional Structures (if-then-else)

All about user-defined episode / movie format expressions
Post Reply
User avatar
rednoah
The Source
Posts: 14639
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: 14639
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: 14639
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.

TrueTenacity
Posts: 8
Joined: 28 Feb 2017, 15:19

Re: Conditional Structures (if-then-else)

Post by TrueTenacity » 17 Sep 2018, 08:31

rednoah wrote:
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.
How would one go about including folders in this?

Movies\Movie Collections\ if there's a collection
Movies\{az} if there isn't one

kim
Power User
Posts: 654
Joined: 15 May 2014, 16:17

Re: Conditional Structures (if-then-else)

Post by kim » 17 Sep 2018, 22:45

Code: Select all

{plex[0]}/{any{'Movie Collections/'+collection}{az}}/{plex.tail}

Code: Select all

{any{'Movie Collections/'+collection.replaceAll('Collection')}{plex[0]+'/'+az}}/{plex.tail}

Code: Select all

{any{'Movie Collections/'+collection.replaceAll('Collection')}{'Movies/'+az}}/{plex.tail}

Code: Select all

{any{'Movie Collections/'+collection.replaceAll('Collection')}{(allOf{'Movies/'}{az}).join()}}/{plex.tail}
EDIT:
viewtopic.php?f=5&t=4796&p=35841#p35841
Last edited by kim on 17 Sep 2018, 23:29, edited 1 time in total.

TrueTenacity
Posts: 8
Joined: 28 Feb 2017, 15:19

Re: Conditional Structures (if-then-else)

Post by TrueTenacity » 17 Sep 2018, 23:00

Thanks!

kim
Power User
Posts: 654
Joined: 15 May 2014, 16:17

Re: Conditional Structures (if-then-else)

Post by kim » 18 Sep 2018, 18:31

TrueTenacity wrote:
18 Sep 2018, 07:46
Hi there,

I used your suggested fix for separating movies and collections, but I seem to have hit a snag...

My current expression is \\gaia-prime\Movies\{any{'Movie Collections/'+az+'/'+collection+'/'+ny+'/'+ny}{ny+'/'+ny}} [{vf}]{subt}

but when i add the az operator to the movies without a collection, the whole thing stops working.

am I adding it wrong or something?
You mean this ?

Code: Select all

\\gaia-prime\Movies\{any{'Movie Collections/'+az+'/'+collection+'/'+ny+'/'+ny}{az+'/'+ny+'/'+ny}} [{vf}]{subt}
sample output you want ?

TrueTenacity
Posts: 8
Joined: 28 Feb 2017, 15:19

Re: Conditional Structures (if-then-else)

Post by TrueTenacity » 18 Sep 2018, 19:38

that's it... it works PERFECTLY for a movie that is in a collection... but when you use it on a movie that isn't in one, it only adds the vf and subt part... the file name and path disappear...

kim
Power User
Posts: 654
Joined: 15 May 2014, 16:17

Re: Conditional Structures (if-then-else)

Post by kim » 19 Sep 2018, 00:32

well if I try this (in GUI)

Code: Select all

\\gaia-prime\Movies\{any{'Movie Collections/'+az+'/'+collection+'/'+ny+'/'+ny}{az+'/'+ny+'/'+ny}} [{vf}]{subt}
I get output
e.g. \\gaia-prime/Movies/D/Demolition Man (1993)/Demolition Man (1993) [720p]
or \\gaia-prime/Movies/Movie Collections/A/Avatar Collection/Avatar (2009)/Avatar (2009) [720p]

try

Code: Select all

\\gaia-prime\Movies\{any{collection ? ('Movie Collections/'+az+'/'+collection+'/'+ny+'/'+ny) : 'collection found but something is wrong'} {az+'/'+ny+'/'+ny} {'WTF'} }
only if I write {az+'/'+ny+'/'+ny} wrong like so {az'/'+ny+'/'+ny} I get the
"but when you use it on a movie that isn't in one, it only adds the vf and subt part... the file name and path disappear..."

btw: there is a better way to write it

Code: Select all

\\gaia-prime\Movies\{any{'Movie Collections/'+az+'/'+collection}{az}}/{ny}/{ny} [{vf}]{subt}
or

Code: Select all

\\gaia-prime\Movies\{def altDir = ('Movie Collections/'+az+'/'); any{altDir+collection}{az}}/{ny}/{ny} [{vf}]{subt}

TrueTenacity
Posts: 8
Joined: 28 Feb 2017, 15:19

Re: Conditional Structures (if-then-else)

Post by TrueTenacity » 19 Sep 2018, 06:54

Kim,

Your better way does the trick!

Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests