If/ElseIf/Else Blocks usage

Discuss and share scripts and script files...
Post Reply
yusef88
Posts: 1126
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

If/ElseIf/Else Blocks usage

Post 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'}

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: If/ElseIf/Else Blocks usage

Post 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>; }
One of my scripts helped you out? Please donate via Paypal

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: If/ElseIf/Else Blocks usage

Post 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>);

yusef88
Posts: 1126
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

Re: If/ElseIf/Else Blocks usage

Post by yusef88 »

as newbie knows a few basics, grateful to learn some new lesson today thank you both highend and TheQwerty

Post Reply