For Loop

By PennyBreed on Mar 06, 2014

This was just a silly thing I was playing with, to implement some sort of for-loop within mIRC.

;Some usage examples..
alias fortest {
  echo -s START!
  ;since this always returns $false, the block of code is ignored by mirc initially.
  if $($for(%on = 1;%on <= 10;inc %on),2) {
    ;do something with %on
    echo -s out! %on
  }
  echo -s END!
}

alias fortest2 {
  echo -s START!
  ;Now with a text file.
  if $($for(%x=1;%x <= $lines(tester.txt);inc %x),2) {
    echo -s out! $read(tester.txt,%x)
  }
  echo -s END!
}

;-------------------- The evaluating aliases.
alias for {
  return $($foreval,0) $+ $chr(40) $+ $($qt($script),0) $+ , $+ $($scriptline,0) $+ , $+ $1 $+ $chr(41)
}

alias foreval {
  var %f = $1
  if ($exists(%f) == $false) { return $false }
  var %o = $calc($2 + 1), %os = $calc($2 + 1)
  var %a = $lines(%f)
  ;instead of reading the input argument, read it from the file unevaluated.
  var %args = $read(%f,n,$2)
  ;parse the parameters
  var %re = if \$\(\$for\(%([^\s=]+)[\s+]?=[\s+]?([^\s;]+);([^;]+);(.+)\),2\)
  var %bc = 1
  noop $regex(%args,%re)
  var % $+ [ $regml(1) ] $regml(2)
  var %wh = $regml(3)
  while ( [ $iif( [ %wh ] ,$true,$false) ] ) {
    while (%o <= %a) {
      var %rline = $read(%f,n,%o)
      var %bc1 = $pos(%rline,$chr(123),0)
      var %bc2 = $pos(%rline,$chr(125),0)
      var %bc = $calc(%bc + $calc($iif(%bc1,%bc1,0) - $iif(%bc2,%bc2,0)))
      if (%bc > 0) {
        $(%rline,2)
      }
      else { break }
      $regml(4)
      inc %o 1
    }
    var %o = %os
    var %bc = 1
  }
}

While this works, I'm certain it falls short in many cases. Namely, the ability to use local scope variables within your loop D: .. However, I was able to use this same theory, to make a shorthand function for looping sqlite results (using mIRC SQLite from http://reko.tiira.net/msqlite/ - which I use fairly often.)

;Here's how it works ..
alias db_test {
  var %db_handle = 1
  if $($dbeach(%db_handle,SELECT word FROM wordlist WHERE LENGTH(word) > 5 LIMIT 5),2) {
    echo -s word: %db.word
  }
}

;Here's how its done ..
alias dbeach {
  return $($dbeval,0) $+ $chr(40) $+ $($qt($script),0) $+ , $+ $($scriptline,0) $+ , $+ $1 $+ , $+ $2- $+ $chr(41)
}

alias dbeval {
  var %f = $1
  var %o = $calc($2 + 1), %os = $calc($2 + 1)
  var %a = $lines(%f)
  if ($exists(%f) == $false) { halt }
  var %dbcon = $3
  var %sql = $4-
  var %bc = 1
  var %res.h = $sqlite_query(%dbcon,%sql)
  while ($sqlite_fetch_row(%res.h,row_ $+ %res.h)) {
    var %ra = $hget(row_ $+ %res.h,0).item
    var %ro = 0
    ;extract the hash table into our scope
    while (%ro <= %ra) {
      if ($hget(row_ $+ %res.h,%ro).item !isnum) {
        var % $+ [ db. $+ [ $hget(row_ $+ %res.h,%ro).item ] ] $hget(row_ $+ %res.h,$hget(row_ $+ %res.h,%ro).item)
      }
      inc %ro 1
    }
    while (%o <= %a) {
      var %rline = $read(%f,n,%o)
      var %bc1 = $pos(%rline,$chr(123),0)
      var %bc2 = $pos(%rline,$chr(125),0)
      var %bc = $calc(%bc + $calc($iif(%bc1,%bc1,0) - $iif(%bc2,%bc2,0)))
      if (%bc > 0) {
        $(%rline,2)
      }
      else { break }
      inc %o 1
    }
    var %o = %os
    hfree row_ $+ %res.h
    var %bc = 1
    unset %db.*
  }
  sqlite_free %res.h
  return $false
}

Comments

Sign in to comment.
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.