Issue with if, else if, else statements

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
explodist
Posts: 2
Joined: 02 Jun 2022, 02:43

Issue with if, else if, else statements

Post by explodist »

I'm having an issue with some conditionals and was hoping someone could point out what my error is.

The file I'm using as reference is

Code: Select all

Blood and Black Lace (6 donne per l'assassino) (1964) (Arrow Restoration).mkv
What I want to achieve: Add "Arrow" or "Criterion" to file names if they are present in the original file name.

My code:

Code: Select all

{ 
if (fn.match(/criterion/)) {
	".Criterion"
	} else if (fn.match(/arrow/)) {
	".Arrow"
	} else { ".none"
	}
}
In the above, if I change "/criterion/" to say "/arrow/", then ".Criterion" is successfully added to the new name. So, it looks like if the first statement is false, the rest of the code isn't hit - it doesn't even hit the else. What am I doing wrong here?
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Issue with if, else if, else statements

Post by rednoah »

:idea: fn.match() either succeeds in matching something, or fails with throwing an error. It will never return false and thus cannot be used for if-then-else conditions:
viewtopic.php?t=1895


e.g. here's what you're trying to do:

Code: Select all

{
	'.' + any{ fn.match(/criterion|arrow/).capitalize() }{ 'none' }
}

e.g. if-then-else would work like this:

Code: Select all

{ 
	if (fn =~ /(?i)criterion/)
		return ".Criterion"
	else if (fn =~ /(?i)arrow/)
		return ".Arrow"
	else
		return ".none"
}
:idea: Please read the FAQ and How to Request Help.
explodist
Posts: 2
Joined: 02 Jun 2022, 02:43

Re: Issue with if, else if, else statements

Post by explodist »

rednoah wrote: 02 Jun 2022, 11:22 e.g. if-then-else would work like this:

Code: Select all

{ 
	if (fn =~ /(?i)criterion/)
		return ".Criterion"
	else if (fn =~ /(?i)arrow/)
		return ".Arrow"
	else
		return ".none"
}
What does (?i) do here? Are these statements identical to using fn.match(), except that they return true or false?
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Issue with if, else if, else statements

Post by rednoah »

=~ is the regex-find operator:
https://docs.groovy-lang.org/latest/htm ... d_operator

(?i) is the case-insensitive flag that makes the regular expression case-insensitive:
https://www.regular-expressions.info/modifiers.html
:idea: Please read the FAQ and How to Request Help.
Post Reply