$shorten, $numdot, and $digit

By Voice of Power on Feb 27, 2004

A few snippets which might prove usefull.

  • $shorten is used to shorten a string, or cut a path
  • $numdot is used to seperate a thousands from hundreds
  • $digit is used to convert a number to a certain amount of digits
/* ====================

The three included aliases in this file are: $shorten, $numdot, and $digit.
These aliases are free for you to use and edit however you want, I made
them because they could be usefull, and I hope they will be. :)

In their current state the aliases can go in the alias section of mIRC, if you
want to put them in the remote section prefix them with 'alias'. You can
remove all comments if you want.

- Voice of Power

===================== */

/*

$shorten - shortens a string, and appends three periods to it, can also cut down a path

Syntax: $shorten(string,length)
  Length is the amount of characters you want to display, including the periods, length has to be bigger than 3.

Properties: right, cut, cutfile, cutfileright
  The cut* properties are used to cut a full path to a file.

Examples:
  //echo -a $shorten(This is a long line of text,10) returns "This is..."
  //echo -a $shorten(This is a long line of text,10).right returns "...of text"
  //echo -a $shorten(C:\Documents and Settings\VoP\Desktop\shorten-numdot-digit.txt,40).cut returns "C:\...\Desktop\shorten-numdot-digit.txt"
  //echo -a $shorten(C:\Documents and Settings\VoP\Desktop\shorten-numdot-digit.txt,10).cut returns "shorten-numdot-digit.txt"
  //echo -a $shorten(C:\Documents and Settings\VoP\Desktop\shorten-numdot-digit.txt,10).cutfile returns "shorten..."
  //echo -a $shorten(C:\Documents and Settings\VoP\Desktop\shorten-numdot-digit.txt,10).cutfileright returns "...git.txt"

*/

shorten {
  if ($isid) {
    ; determine if user wants to cut a path, and if it is long enough for that
    if (cut* iswm $prop) && ($len($1) > $2) {
      ; %i is used for the loop, starting at 2 to preserve the HD letter (C:\)
      ; %p will be used to cut the path, is now filled with the whole path
      ; %n is filled with the length the path should be cut to
      ; %t is filled with the amount of tokens seperated with "\" found in the string
      var %i = 2,%p = $1,%n = $2,%t = $numtok($1,92)

      ; loop while %i is smaller then the amount of tokens
      while (%i <= %t) {
        ; replace the left most token with "...", but before that remove any "...\" found, to prevent a result like: C:\...\...\...\file.txt
        %p = $replace($remove(%p,...\),$gettok($1,%i,92),...)

        ; if the path is too short (ie. C:\...), the filename should be returned
        if ($right(%p,3) == ...) {
          ; determine if the user wants to shorten the filename
          if ($len($nopath($1)) > $2) && (*file* iswm $prop) {
            ; shorten the filename left or right, and return it
            if (*right iswm $prop) { return ... $+ $right($nopath($1),$calc($2 - 3)) }
            return $left($nopath($1),$calc($2 - 3)) $+ ...
          }
          ; else return the whole filename
          return $nopath($1)
        }
        ; if %p is short enough to be returned, return it
        elseif ($len(%p) <= $2) { return %p }
        inc %i
      }
    }

    ; if no path is to be cut, just shorten the string. first determine if the string is longer then the length or not, and if length is bigger than 3
    elseif ($2 > 3) && ($len($1) > $2) {
      ; shorten the string left or right, and return it
      if ($prop == right) { return ... $+ $right($1,$calc($2 - 3)) }
      return $left($1,$calc($2 - 3)) $+ ...
    }
    ; else return string
    return $1
  }
}

/*

$numdot - seperates thousands from hundreds, with the character you supply (optional)

Syntax: $numdot(N,[C])
  N is the number you want to split, C is optional and is the ascii character you want to split N with. When C is not given a period will be used.

Examples:
  //echo -a $numdot(100) returns "100"
  //echo -a $numdot(1000) return "1.000"
  //echo -a $numdot(1000.00) returns "1.000,00"
  //echo -a $numdot(817425394) returns "817.425.394"
  //echo -a $numdot(1000,44) returns "1,000"
  //echo -a $numdot(1000,32) returns "1 000"
  //echo -a $numdot(1000.00,44) returns "1,000.00"

*/

numdot {
  ; $1 should be a number
  if ($isid) && ($1 isnum) {
    ; %i is filled with how many times should be looped
    ; %n is filled with the number to split, without decimals
    ; %c is the character to split the number with, defaults to a period
    ; %c2 is the character to use for decimals, when %c is a period, %c2 will be a comma, otherwise a period
    ; %t and %d are defined for later usage
    var %i = $round($calc($len($1) / 3),0),%n = $gettok($1,1,46),%c = $chr($iif($2 isnum 1-255,$ifmatch,46)),%c2 = $iif(%c == $chr(46),$chr(44),$chr(46)),%t,%d

    while (%i) {
      ; if the length of %n is 3, break
      if ($len(%n) <= 3) { break }
      ; set the three right most numbers in %n to %d
      %d = $right(%n,3)
      ; remove the three right most numbers from %n
      %n = $left(%n,-3)
      ; while %n becomes shorter and shorter, %t becomes longer and longer, %t contains the split line
      %t = %c $+ %d $+ %t
      dec %i
    }
    ; return what's left of %n, with %t after it, and last the decimals, if any
    return %n $+ %t $+ $iif($gettok($1,2,46),%c2 $+ $ifmatch)
  }
}

/*

$digit - convert a number to a certain amount of digits

Syntax: $digit(N,D)
  N is a number, D is the amount of digits you want returned

Examples:
  //echo -a $digit(1,2) returns "01"
  //echo -a $digit(10,1) returns "10"
  //echo -a $digit(0001,2) returns "01"
  //echo -a $digit(0101,2) returns "101"

*/

digit {
  ; both $1 and $2 should be a number
  if ($isid) && ($1 isnum) && ($2 isnum) {
    ; if the length of $1 is smaller then $2, prefix $1 with enough 0's so it will match $2
    if ($len($1) < $2) { return $str(0,$calc($2 - $len($1))) $+ $1 }
    ; if the length of $1 is bigger then $2
    elseif ($len($1) > $2) {
      ; set $2 to %l or it will be forgotten after the /tokenize coming up
      var %l = $2
      ; while the left most number is a zero
      while ($left($1,1) == 0) {
        ; remove the left most number from $1, and set the result to $1 again with /tokenize
        tokenize 32 $right($1,-1)
        ; if $1's length matches $2, break
        if ($len($1) == %l) { break }
      }
    }
    ; pretty self-explanatory
    return $1
  }
}

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.