$ieif

By Meta on Aug 11, 2011

UPDATE: Complete rewrite of the code. It's not pretty, but so far it works. Uses only two if statements. $v1 and $v2 appear to be working correctly thus far, unless used within the identifier. Thanks to SReject for helping on #msl. I have yet to test everything jaytea's mentioned.

A simple but useful alias (hopefully) that mimics $iif while adding "elseif" functionality. I believe there was a challenge like this somewhere, but I have no idea where...


Syntax: **$ieif(C1,V1[,C2,V2,...][,E])**[.true|false]

Notes: [list=1]
[*]If no property is used, returns the value (V#) following the first TRUE conditional (C#) (i.e. non-0/$false/$null)

[*]Properties true and false return the number of true or false conditionals, respectively. Any other property is ignored. (i.e. returns the same value that would be returned without one)

[*]If no conditionals are true and $2 is not $null: Returns E if $0 is odd, else $null

[*]Every odd parameter except for the final (if $0 is odd) is seen as a conditional (C#), while every even parameter is seen as a return value (V#)
[/list]

Examples:

  • $ieif(1 == 1,A,2 == 2,B) --> A

  • $ieif(1 == 2,A,2 == 2,B) --> B

  • $ieif(1 == 1,A,2 == 1,B,3 == 1,C).true --> 1

  • $ieif(1 == 1,A,2 == 1,B,3 == 1,C).false --> 2

  • $ieif(1 == 2,A) --> $null

  • $ieif(1 == 2,A,B) --> B

  • $ieif(1 == 1) --> $null

  • $ieif(1 == 1,A).blah --> A

Bugs:

[list=1]
[*]Some bugs involving commas, parenthesis, and possibly other similar characters.

[*]$v1 and 2 don't evaluate when used within the identifier.
[/list]

alias ieif {
  var %i 1, %t 0, %f 0, %r /\b(\$)(\S+)/g, %s \1!\2 %t 0, %f 0, %b $replacex($and(1,$0),1,$0,0,$calc(1+$0))
  :L
  var %e $regsubex($replace($and(1,$0),1,$ [ $+ [ $0 ] ],0,),%r,%s), %c $regsubex($ [ $+ [ %i ] ] ,%r,%s), %v  $ [ $+ [ $calc(1+%i) ] ]
  $iif(!$istok(true false,$prop,32) && [ %c ] ,return %v,inc %i 2)
  $iif( [ %c ] ,inc %t,inc %f)
  goto $replacex(%i,%b,E $chr(32),%i,L $chr(32))
  :E
  var %p $replace($prop,true,%t,false,%f)
  return $gettok(%p %e,1,32)
}

Comments

Sign in to comment.
Dani_l11   -  Aug 16, 2011

Make your version jaytea!

 Respond  
jaytea   -  Aug 11, 2011

i really like these kind of ideas ;) unfortunately, they're usually too difficult (and in this case, impossible) to get right owing to the 'special' nature of mIRC's interpreter.

when you use $iif(EXP, t, f), mIRC takes the first parameter 'EXP' and inserts it, without any evaluation, directly into an if statement as 'if (EXP)'. that's how $iif($(1 == 2), y, n) is able to return 'y' (with $v1 = '1 == 2'). but, as you've probably discovered, there's no way to determine whether the user passed '1 == 2' or '$(1 == 2)' as the condition.

then there's the issue of double evaluations, which you've quite admirably attempted to handle by inserting those '!' chars. but what about variables, evaluation brackets, parentheses, commas and, on the flip side, '$' used as part of plaintext? ;P you could get around all this by first dissecting the condition manually: find the operator and take everything on the left as v1 and everything on the right (if it exists) as v2. then you can set up the condition as '%v1 op %v2' such that '%v1' and '%v2' evaluate to produce the given values. only bad thing about this is you'll have to do a bit of tedious work to accommodate multiple conditions separated by && and/or || in a single parm. could be a fun exercise ;D

about that $v1 / $v2 problem: i'm not sure what you would expect $v1 and $v2 to return if $prop is 'true' or 'false', but if you switch '[ %x ]' and '!$prop' around on that 5th line, and transform the last line into a couple of if statements to force $v1 and $v2 to be filled in the 'else' case, $v1 and $v2 will be filled correctly after a call to $ieif(). having $v1 and $v2 filled inside a call to $ieif() is not possible though, since the args passed to $ieif() will always evaluate before the alias is called.

 Respond  
SReject   -  Aug 11, 2011

My Version:

alias ieif {
  var %x = 1, %b
  while (%x <= $0) {
    %b = $($ $+ %x,2)
    if (2 \\ $0) && ($calc($0 - 2) == %x) {
      return $iif( [ %b ] , [ $(,$ $+ $calc(%x +1),2) ] , [ $(,$ $+ $calc(%x +2),2) ] )
    }
    $iif( [ %b ] ,return [ $(,$ $+ $calc(%x +1),2) ] , inc %x 2)
  }
}

Usage: Same as $iif() except it allows for multiple elseifs.
IE: $ieif(a == a,a,b == b,b,c)

Issues/Bugs:
For things you want evaluated from within the $ieif() you must $(...,0) those items(such as $v1 and $v2)
For things you don't want evaluated at all, you must double escape them. IE: $ieif(a == a,$($!me is testing,0))

Fixes: Fixed the elsepart not evaluting $v1/$v2/$ifmatch/$ifmatch2 properly

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.