For Loop

By kibo on Oct 26, 2011

In MSL (mirc scripting language) is not expected the for loop.so i have decided to create a for loop using aliases a some variables.

alias for {
  var %n = $1
  if (%n isnum) {
    while (%n != 0) {
      $2-
      dec %n
    }
  } 
  else { for.error }
}

alias try_for {
  $for($1,echo -s Hello World!!!)
}

alias -l for.error { echo -s 4Error in For Loop }

Comments

Sign in to comment.
kibo   -  Oct 29, 2011

I would like to customize the error message, but it's just a fad. It would be an unnecessary work.

 Respond  
SReject   -  Oct 28, 2011

why change my code kibo? $try() is the error handler, all you have to do is check if it != $Null

$try() - Tries to execute and returns the error message if errors. Otherwise it returns $Null

alias try $1- | if ($error) reseterror | return $v1

Example:

alias try-example {
  echo 12 -a Trying: "/echo -a test" Result: $try(echo -a Test)
  echo 04 -a Trying: "/echo -a" Result: $try(echo -a)
}
 Respond  
kibo   -  Oct 28, 2011

for example i can make a error handler.

alias handle::error {
;error echo debug
if (echo isin $1-) { echo -s 4 echo command error | halt }
;error window debug
if (window isin $1-) { echo -s 4 window command error | halt }
;etc etc
}

alias test_123 {
; if (!$1) { echo -s command does not exist | halt }
$1-
:error
if ($error) {
reseterror
handle::error $v1
}
}

 Respond  
Jethro   -  Oct 28, 2011

What module is that? mIRC already has the commands and identifier :error , $error and reseterror to handle the error messages generated by a faulty code.

 Respond  
kibo   -  Oct 28, 2011

Srjeject I have an idea about a module for error handling in mirc. I sketched something use your technique for handling errors.

 Respond  
SReject   -  Oct 28, 2011

Thanks kibo. :)

 Respond  
SReject   -  Oct 28, 2011

Jethro, b/c $error is evaluated and stored in $v1 before /reseterror is called

 Respond  
kibo   -  Oct 28, 2011

Sreject your code is beautiful and elegant. compliments

 Respond  
Jethro   -  Oct 27, 2011

If you reset the error first, how do you return $v1? The /reseterror commands sort of halts or suppresses it.

 Respond  
SReject   -  Oct 27, 2011

You could use it as an identifer so to speak, this way if there is an error, the calling script will be able to handle it:

alias for {
  if ($0 != 2 || $1. !isnum 0- || $2 == $null) return * /For: Invalid parameters

  var %n = $1
  while (%n) {
    if ($try($2-)) return $v1 
    dec %n
  }

}
alias try $1- | :error | if ($error) reseterror | return $v1

;---------------;
;--- Example ---;
;---------------;
alias for:exp {

  ;These two will throw errors!
  if ($for(a,)) echo -a Error: $v1
  if ($for(1,!write)) echo -a Error: $v1

  ;if $for(,) returns null, it succeeded
  if (!$for(2,echo -a hey!)) echo -a Success!
}

Edited to add some more error checking & to clean up the code

 Respond  
kibo   -  Oct 27, 2011

I prefer to use a Private Alias so i can a more structured code, even if your code is more optimized, because it does not call an Alias so is more fast.

 Respond  
Fuzionx   -  Oct 27, 2011

If you want an error message then you should use this:

alias for {
  var %n = $1
  if (%n isnum) {
    while (%n != 0) {
      $2-
      dec %n
    }
  }
  else {
    echo 4 -as Error in For Loop
  }
}
 Respond  
kibo   -  Oct 27, 2011

ok I'll use your solution . I now regard a "like" :P

 Respond  
Fuzionx   -  Oct 27, 2011

Your solution is far from compact and efficient.

 Respond  
kibo   -  Oct 27, 2011

I preferred to use a less compact solution

 Respond  
Fuzionx   -  Oct 27, 2011

You might want to check if $1 is a number:

while (%n isnum && %n != 0) {
 Respond  
kibo   -  Oct 27, 2011

Thank you. I have to improve it with a check on the $1 parameter.

 Respond  
alabama   -  Oct 26, 2011

nice

 Respond  
kibo   -  Oct 26, 2011

Tnx . i'have edit it.

 Respond  
FelicianoX   -  Oct 26, 2011

You can use /dec %n to decrease %n instead of using $calc(%n -1)

 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.