Page 1 of 1
If/ElseIf/Else Blocks usage
Posted: 15 Mar 2017 07:58
by yusef88
Hi.. can't that script be shorter than this?
Code: Select all
$e = get("curitem", "ext");
if ($e == srt) {rename b, '<clipboard>.en'}
elseif ($e == ass) {rename b, '<clipboard>.en'}
elseif ($e == ssa) {rename b, '<clipboard>.en'}
else {rename b, <clipboard> };
instead of repeating the elseif line, the "if" line becomes
Code: Select all
if ($e == srt == ass == ssa) {rename b, '<clipboard>.en'}
Re: If/ElseIf/Else Blocks usage
Posted: 15 Mar 2017 08:52
by highend
You are doing string comparisons, put your strings into single / double quotes please...
For exact matches:
Code: Select all
if ("|srt|ass|ssa|" LikeI "*|$e|*") { rename b, '<clipboard>.en'; }
else { rename b, <clipboard>; }
For lazy matches (but these could match on other occasions...)
Code: Select all
if ("srt|ass|ssa" LikeI "*$e*") { rename b, '<clipboard>.en'; }
else { rename b, <clipboard>; }
Code: Select all
if (regexmatches($e, "srt|ass|ssa")) { rename b, '<clipboard>.en'; }
else { rename b, <clipboard>; }
Re: If/ElseIf/Else Blocks usage
Posted: 15 Mar 2017 13:05
by TheQwerty
It also depends on what you actually mean by "shorter".
If you mean fewest lines of code then all of highend's suggestions are good but I'd add the obvious:
Code: Select all
$e = get("curitem", "ext");
if ($e == 'srt' || $e == 'ass' || $e == 'ssa') {rename b, '<clipboard>.en'}
else {rename b, <clipboard> };
And I might prefer GetTokenIndex...
Code: Select all
rename 'b', <clipboard> . (GetTokenIndex(<curext>, 'srt|ass|ssa',, 'ic') ? '.en' : '');
If you want less complication then using the recently introduced switch-case is a great choice:
Code: Select all
function NewExt($e) {
switch (Recase($e,'l')) {
case 'srt':
case 'ass':
case 'ssa':
return '.en';
default:
return '';
}
}
rename 'b', <clipboard> . NewExt(<curext>);
Re: If/ElseIf/Else Blocks usage
Posted: 15 Mar 2017 14:24
by yusef88
as newbie knows a few basics, grateful to learn some new lesson today thank you both highend and TheQwerty