SReject commented on a Page, For Loop  -  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  
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.