Arrays for mIRC — File Browser

ArraysformIRCv1.04 / ArraysformIRCv1.04 / hasharray / Arrays.mrc

Arrays.mrc — 7.11 KB — Download this file

;; Arrays for mIRC
;; Scripted By: Naquada
;; Version: 1.04

;; $array(0)                     - Total number of arrays
;; $array(index)                 - Returns the name of the array at index 
;; $array(array, 0)              - Length of the array
;; $array(array, index)          - reference string in the array

;; $array(array, findtext, N)    - returns the array index of the N'th numbered match
;;                                 to findtext

;; /array -f array               - frees the array from memory and removes saved file
;; /array -a array string        - adds a string to the end of an array
;; /array -i index array string  - inserts a item into the array
;; /array -d index array         - deletes an item from the array
;; /array -r index array         - replaces an item from the array
;; /array -t array               - sort the array, ascending
;; /array -T array               - sort the array, decending
;; /array -s array               - saves the array to a predefined local file
;;                                 can be used alone or with another switch
;;
;; Notes
;; =====
;; * Arrays are saved on EXIT only... To prevent the lose of data from mIRC crashing
;;   you should save your arrays (/array -s) after making changes to them. Although
;;   if you are making 100's of changes do it only once after that final change.
;;
;; * Arrays must remain sequential... An insert at a point larger than the current
;;   array will place the item at the end of the array. A replace and delete at a
;;   point larger than the current array will fail.

alias testarray {
  clear
  array -f test
  array -a test Cat
  array -a test Zat
  array -a test Mat
  array -a test Hat
  array -a test Fat
  array -r 1 test Pat
  array -d 3 test
  array -i 3 test Sat
  array -t test
  echo -a Sorted acending... (-t)
  echo -a $array(test, 1) (should be Fat)
  echo -a $array(test, 2) (should be Hat)
  echo -a $array(test, 3) (should be Pat)
  echo -a $array(test, 4) (should be Sat)
  echo -a $array(test, 5) (should be Zat)
  array -Ts test
  echo -a Sorted decending... (-T)
  echo -a $array(test, 1) (should be Zat)
  echo -a $array(test, 2) (should be Sat)
  echo -a $array(test, 3) (should be Pat)
  echo -a $array(test, 4) (should be Hat)
  echo -a $array(test, 5) (should be Fat)
  echo -a There are $array(test, 0) elements in the test array
  echo -a There $iif($array(0) == 1, is, are) $array(0) array $+ $iif($array(0) != 1, s) in memory
}

alias array {
  var %Table | var %Index | var %Count | var %Stop | var %Save $false
  if ($isid) && ($2 == $null) return $arrayid($1)
  if ($1 == $null) || ($2 == $null) return
  if ($isid) return $arrayid($getname($1), $2, $3)
  if (s isin $1) { set %Save $true | tokenize 32 $remove($1, s) $2- }
  if (($1 == -t) && ($hget($getname($2)))) {
    set %Table $getname($2)
    var %i = 1, %j
    var %size $arrayid(%Table, 0)
    while (%i <= %size) {
      set %j $calc(%i + 1)
      while (%j <= %size) {
        var %iVal = $arrayid(%Table, %i)
        var %jVal = $arrayid(%Table, %j)
        if (($isupper($1)) && (%iVal < %jVal)) || ((!$isupper($1)) && (%iVal > %jVal)) {
          hdel %Table %i
          hdel %Table %j
          hadd %Table %i %jVal
          hadd %Table %j %iVal
        }
        inc %j
      }
      inc %i
    }
    if (%Save) savetofile %Table
  }
  else if (($1 == -f) && ($hget($getname($2)))) {
    set %Table $getname($2)
    if (* isin $2) hfree -w $getname($2)
    else hfree $getname($2)
    if ($isfile(arrays\ $+ %Table)) .remove "arrays\ $+ %Table $+ "
  }
  else if ($1 == -a) {
    set %Table $getname($2)
    set %Index $calc($arrayid(%Table, 0) + 1)
    hadd -m %Table %Index $3-
    if (%Save) savetofile %Table
  }
  else if (($1 == -r) && ($hget($getname($3)))) {
    set %Table $getname($3)
    set %Index $2
    set %Count $arrayid(%Table, 0)
    if (%Index <= %Count) && (%Index > 0) && (%Index > 0) {
      hdel %Table %Index
      hadd %Table %Index $4-
      if (%Save) savetofile %Table
    }
    else if ($show) { echo -a Array: Invalid index specified with -r }
  }
  else if (($1 == -d) && ($hget($getname($3)))) {
    set %Table $getname($3)
    set %Count $arrayid(%Table, 0)
    set %Index $2
    if ((%Index > %Count) || (%Index < 1)) && ($show) {
      echo -a Array: Invalid index specified with -d
      return
    }
    while (%Index < %Count) {
      hdel %Table %Index
      hadd -m %Table %Index $arrayid(%Table, $calc(%Index + 1))
      inc %index
    }
    hdel %Table %Index
    if (%Save) savetofile %Table
  }
  else if (($1 == -i) && ($hget($getname($3)))) {
    set %Table $getname($3)
    set %Index $arrayid(%Table, 0)
    if ($2 < %Index) {
      set %Stop $2
      while (%Index >= %Stop) {
        hadd %Table $calc(%Index + 1) $arrayid(%Table, %Index)
        dec %Index
      }
      inc %Index
    }
    else set %Index $calc($arrayid(%Table, 0) + 1)
    hadd -m %Table %Index $4-
    if (%Save) savetofile %Table
  }
  else if (%Save) && ($hget($getname($2))) { 
    set %Table $getname($2)
    savetofile %Table
  }
  else if ($1 != -f) && ($show) { echo -a Array: Invalid Syntax or No such Array }
  ;; We can silently fail on non-existent tables when -f is used
}

alias -l savetofile {
  var %Table $1
  if (!$isdir(arrays)) mkdir arrays
  hsave -b %Table "arrays\ $+ %Table $+ "
}

alias -l getname { return $iif($1, Array. $+ $1, $null) }
alias -l arrayid {
  if ($3 isnum) return $hfind($1, $2, $3).data
  if ($1 isnum) && ($2 == $null) return $arraynames($1)
  if (!$hget($1)) { if ($2 == 0) return 0 | return $null }
  if ($2 == 0) return $hget($1, 0).item
  return $hget($1, $2)
}
alias -l arraynames {
  var %Count 0 | var %Index 1 | var %HashTable | set %HashTable $hget(%Index)
  while (%HashTable) {
    if ($left(%HashTable, 6) == Array.) {
      inc %Count | if (%Count == $1) return $right(%HashTable, -6)
    }
    inc %Index | set %HashTable $hget(%Index)
  }
  if ($1 == 0) return %Count
}
alias makeandloadtable {
  if (!$hget($1)) { .hmake $1 20 | .hload -b $1 "arrays\ $+ $1 $+ " }
}
on *:START:{
  noop $findfile(arrays,Array.*,0,1, makeandloadtable $nopath($1-) )
}
on *:EXIT:{
  var %index 1 | var %table $hget(1)
  noop $findfile(arrays,Array.*,0,1, .remove "arrays\ $+ $nopath($1-) $+ " )
  while (%table) {
    if ($left(%table, 6) == Array.) {
      if (!$isdir(arrays)) mkdir arrays
      hsave -b %table "arrays\ $+ %table $+ "
    }
    inc %index | set %table $hget(%index)
  }
}

;; Updates, Version 1.04
;;    * Fixed ability to save with sort switch

;; Updates, Version 1.03
;;    * Added support for finding an index to a particular array element using
;;      wildcard matching text
;;    * Added support for sorting the array
;;    * Streamlined loading of saved arrays
;;    * Removed requirement that -s preceed other switches

;; Updates, Version 1.02
;;    * Added the -r replace argument
;;    * Added support for getting array names with $array(N)
;;    * Added support for wildcards with the -f argument
;;    * Added a fail message to the -d argument for an invalid index value