By brinxy on Nov 14, 2007

Snippet that removes multiple tokens from a string of text. An enhancement to $remtok.

;; --- $multiremtok by brinxy --- ;;
;; About ;;
;; $multiremtok is an enhanced version of $remtok allowing you to remove multiple tokens at once from a string of text. ;;
;; -------------------- ;;
;; Snippet Syntax ;;
;; -------------------- ;;
;; $multiremtok([string],[n],[n*],[C]) 
;;  Breaking it down:
;;      - [string]: String to maniipulate
;;      - [n]: the first Nth token to be removed
;;      - [n*]: The Nth Nth token to be removed
;;      - [C]: Common Character
;; -------------------- ;;
;; Examples ;;
;;  - $multiremtok(this is a string of text,1,2,3,32) returns 'string of text' because it removed tokens 1-3 (this, is, and a)
;;  - $multiremtok(this is a string of text,1-3,4,32)  returns 'of text' because it removes tokens 1-3, as specified, and token 4 (string)
;; Counter examples ;;
;;  - $multiremtok(this is a string of text,233,3,32) returns $false because 233 isn't a token in the string
;;  - $multiremrok(this is a string of text,a,4,32) returns $false because a isn't a number
;;  - $multiremtok(this is a string of text,4-a,2,32) returns this is of text because it ignores '-a' 
;; -------------------- ;;
;; Notes ;;
;; -------------------- ;;
;; Do you now how, in the first counter example, the string returned $false? 
;;  - If you put '233' to not be the first token, the snippet will remove all tokens before it reaches 233.
;;      - Once it reaches 233, the while event breaks and it doesn't replace anymore.
;; This rule applies for letters as the removed token as well.

alias multiremtok { 
  if ($$1) { 
    var %0 = $($+($,$0),2), %inc = 2,%get = $1,%d
    while (%inc < $0) { 
      if ($($+($,%inc),2) > $numtok(%get,%0)) { break }
      elseif (!$gettok(%get,$($+($,%inc),2),%0)) { break } 
      elseif (!$regex($($+($,%inc),2),/^[0-9]+/gi)) { break }
      else { %d = $addtok(%d,$gettok(%get,$($+($,%inc),2),%0),%0) }
      inc %inc
    if (%d) { 
      var %x = 1
      while ($gettok(%d,%x,%0)) { 
        %get = $remtok(%get,$v1,1,32)
        inc %x
      return %get
    else return $false


Sign in to comment.
APtrackz   -  Mar 28, 2008

I\'m very late on this, but perhaps have it ignore the invalid param using /continue? It\'d be great to implement this VS. stopping the entire process. Otherwise, fantastic work.

brinxy   -  Nov 15, 2007

Thanks spitfire3292.

spitfire3292   -  Nov 15, 2007

Very well done. It took me a second to figure out why you used regex until I looked back up at all the params that can be used. What I really like is how you can \"ignore\" a param with a certain specific phrase. Very clever and very well thought out to create very little room for error alphanumeric-param wise :P

brinxy   -  Nov 15, 2007

Thanks, it\'s quite simple if you\'re fluent in token identifiers:
Let\'s say I had the string \"this is my string of tokens\"...
Now if we typed up //echo -a $multiremtok(this is my string of tokens,1,2,32) would return \"my string of tokens\" Because it removed the first token, this, and the second token, is.

Any more questions you have feel free to ask and I\'ll be glad to help. Although this should have been covered in the comments I put into the snippet.

SpotRedDog   -  Nov 14, 2007

Nice although I don\'t understand how to use it but it looks nice. WTG!!!

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.