Conditional Structures (if-then-else)

All about user-defined episode / movie format expressions
User avatar
rednoah
The Source
Posts: 18539
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: 18539
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: 30
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: 18539
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: 14
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: 982
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: 14
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: 982
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: 14
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: 982
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: 14
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!

aryanprincesanty
Posts: 2
Joined: 24 Mar 2020, 05:32

Re: Conditional Structures (if-then-else)

Post by aryanprincesanty » 24 Mar 2020, 05:41

Can someone help me with making an if else condition for {group}? I have movies from 2 main groups, Tigole and YTS (YTS.LT, YTS.MX, YTS.AM). I want my files to look like:

Code: Select all

 8 Mile (2002) [1080p BLURAY x265 10-bit AAC 5.1 QxR] 

Code: Select all

 Ad Astra (2019) [1080p WEBRIP x264 8-bit AAC 2.0 YTS.LT] 
I want it such that if file contains Tigole or QxR, I want it to have Tigole in filename. If file contains YTS.LT, YTS.MX, YTS.AM or nothing, I want it to have only YTS in filename.

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

Re: Conditional Structures (if-then-else)

Post by rednoah » 24 Mar 2020, 05:57

e.g. if file name contains Tigole, yield Tigole, otherwise yield YTS:

Code: Select all

fn =~ /Tigole|QxR/ ? 'Tigole' : 'YTS'
:idea: Please read the FAQ and How to Request Help.

AbedlaPaille
Posts: 90
Joined: 12 Apr 2020, 04:02

Re: Conditional Structures (if-then-else)

Post by AbedlaPaille » 27 Apr 2020, 02:25

kim wrote:
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}
Very interesting this! Is it possible to insert conditional trails within the paths? Like a vf.match in the directory name, can't seem to adapt my normal expression. Even when i got the syntax right it crashed paths for movies that didn't match vf

Code: Select all

{any{'[Collections]/'+collection+'/'+'('+y+')'+' '+n}{ny+' ['+vf.match(/720[pP]|1080[pP]/)+']'+'/'+ny}}
I don't know how to play around Primary title and n either, usually use something like this:

Code: Select all

info.OriginalLanguage =~ /en/ ? primaryTitle : n

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

Re: Conditional Structures (if-then-else)

Post by kim » 27 Apr 2020, 17:10

Code: Select all

{ any{n}{ny}{'no match'} }

AbedlaPaille
Posts: 90
Joined: 12 Apr 2020, 04:02

Re: Conditional Structures (if-then-else)

Post by AbedlaPaille » 02 May 2020, 05:18

Didn't get that sorry, bit of a noob. How do you suggest i write info.OriginalLanguage =~ /en/ ? primaryTitle : n if it's within a {any{'/'/' }{'/'/' }} path? Most syntax i tried don't seem to be working. And what am i missing when it comes to displaying potential attributes on folders like i do with my normal scheme?

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

Re: Conditional Structures (if-then-else)

Post by rednoah » 02 May 2020, 07:28

e.g. Here's what you asked for specifically:

Code: Select all

{any{info.OriginalLanguage =~ /en/ ? primaryTitle : n}{'No Name'}}

e.g. Here's how you'd work with potential attributes:

Code: Select all

{allOf{vc}{vf}{source}{group}.joining('-', '[', ']')}
:idea: Please read the FAQ and How to Request Help.

AbedlaPaille
Posts: 90
Joined: 12 Apr 2020, 04:02

Re: Conditional Structures (if-then-else)

Post by AbedlaPaille » 27 May 2020, 12:30

Often find myself in cases where i could use either {any} or { x ? x : y } pretty much interchangeably. In those cases is there a better option ? Like one less hungry in computing power ?

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

Re: Conditional Structures (if-then-else)

Post by rednoah » 27 May 2020, 12:53

I'd use the one that is more readable. They're not interchangeable though, because the ternary operator does not catch exceptions, and because it does less, I'd expect it to be faster, but definitely not noticeably faster in human time scales.
:idea: Please read the FAQ and How to Request Help.

AbedlaPaille
Posts: 90
Joined: 12 Apr 2020, 04:02

Re: Conditional Structures (if-then-else)

Post by AbedlaPaille » 27 May 2020, 15:06

Cheers. So the complex format i posted earlier is pretty sound and i wouldn't be able to optimize its speed much more, correct?

For reference viewtopic.php?f=5&t=11862

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

Re: Conditional Structures (if-then-else)

Post by rednoah » 27 May 2020, 15:12

AbedlaPaille wrote:
27 May 2020, 15:06
So the complex format i posted earlier is pretty sound and i wouldn't be able to optimize its speed much more, correct?
I'm sure it's possible to optimize speed, and the first step would to profile each part, to see which one is slower than expected. As with everything, the 20 / 80 principle applies, 20% of your code will be responsible for 80% of run time, so it's integral to find out what those 20% are first.
:idea: Please read the FAQ and How to Request Help.

AbedlaPaille
Posts: 90
Joined: 12 Apr 2020, 04:02

Re: Conditional Structures (if-then-else)

Post by AbedlaPaille » 27 May 2020, 15:14

Thanks for the pointers !

nomadicleo
Posts: 2
Joined: 27 Jun 2020, 14:52

Re: Conditional Structures (if-then-else)

Post by nomadicleo » 27 Jun 2020, 14:57

Hi,

Could someone please help me with a conditional statement for (certification). My original preset is:

Code: Select all

{n.replaceAll(/[:|]/, " - ")} ({y})\{n.replaceAll(/[:|]/, " - ")} ({y}) {vf} - {certification}
When it does not find a certification, it simply appends the hyphen at the end of the name but I would like it to only add the hyphen when there is a certification found.

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

Re: Conditional Structures (if-then-else)

Post by rednoah » 27 Jun 2020, 15:10

nomadicleo wrote:
27 Jun 2020, 14:57
When it does not find a certification, it simply appends the hyphen at the end of the name but I would like it to only add the hyphen when there is a certification found.
e.g.

Code: Select all

{" - " + certification}
:arrow: viewtopic.php?t=1895
:idea: Please read the FAQ and How to Request Help.

Post Reply