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 }
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)
}
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
}
}
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