$lentok()

By Tokensoup on Jan 03, 2008

$lentok() generates a sorted list of tokens based on length. From this list of tokens, you specify N to return the Nth token (the syntax imitating that of $gettok [e.g 1-2 or 1- or 1]). Comes with a commented version, for educational purposes, or a non commented version for those of you who don't need comments.


Syntax


$lentok(string,N,C)[.lrp] (Each property is optional)


l returns the length of the Nth token.


r lists the tokens from greatest to least (default is least to greatest)


p used with l, this will return the length of the Nth token AND the token (: [e.g 9:Tokensoup])


Comments, flames, criticism, go for it. ;P


Cheers.

;;;; COMMENTED VERSION ;;;;

alias lentok { 
  if ($3) { 
    var %x = 1, %r = $1   
    while ($gettok(%r,%x,$3)) {

      ; begin loop, this will get our string to sort

      %r = $reptok(%r,$v1,$+($len($v1),$cr,$v1),1,$3)

      ; Replace Nth token in the string with the length of the token $&
        ; + the token so we can sort. $&
        ; $reptok will be used to replace the token ($v1) with $&
        ; the length of the token + the token [$+($len($v1),$cr,$v1)]

      inc %x
    }
    var %p = $iif(r isin $prop,nr,n), %r = $sorttok(%r,$3,%p), %x = 1

    ; %p will define our way of sorting: $&
      ; if 'r' is in the prop, it will sort from greatest to least $&
      ; else it will sort from least to greatest

    ; %r will sort the string into a variable (%r) based on %p

    while ($gettok(%r,%x,$3)) { 

      ; now we will remove all the stuff we don't want $&
        ; (the previously added length)

      %r = $puttok(%r,$deltok($v1,1,13),%x,$3)

      ; we do the same as the above loop, but this time removing $&
        ; the length from the token by putting the removed $&
        ; length from the token ($v1) [$deltok($v1,1,13)]

      inc %x
    }
    var %f = $iif($2 == 0 && l isin $prop,1,$2), %g = $gettok(%r,%f,$3)

    ; %f will be used to return the Nth token in the list $&
      ; if %f is 0 and l isin the prop we return 1 (The first token) or $&
      ; N (what you specify or $2) 

    return $iif(l isin $prop,$iif(p isin $prop,$+($len($remove(%g,$chr($3))),:,%g),$len($remove(%g,$chr($3)))),%g)

    ; now, using %f, we return the output (%g) varying with the props we use: $&
      ; if we specified l & p, we can return special output that the author $&
      ; specified in the description of the snippet
  }
}

;;;; NON COMMENTED VERSION ;;;;

alias lentok { 
  if ($3) { 
    var %x = 1, %r = $1   
    while ($gettok(%r,%x,$3)) {
      %r = $reptok(%r,$v1,$+($len($v1),$cr,$v1),1,$3)
      inc %x
    }
    var %p = $iif(r isin $prop,nr,n), %r = $sorttok(%r,$3,%p), %x = 1
    while ($gettok(%r,%x,$3)) { 
      %r = $puttok(%r,$deltok($v1,1,13),%x,$3)
      inc %x
    }
    var %f = $iif($2 == 0 && l isin $prop,1,$2), %g = $gettok(%r,%f,$3)
    return $iif(l isin $prop,$iif(p isin $prop,$+($len($remove(%g,$chr($3))),:,%g),$len($remove(%g,$chr($3)))),%g)
  }
} 

Comments

Sign in to comment.
APtrackz   -  Jan 04, 2008

What purpose does this serve? Jc.

 Respond  
Tokensoup   -  Jan 03, 2008

Snippet updated.

Any other input I can get would be great.

Regards.

 Respond  
Tokensoup   -  Jan 03, 2008

Thanks for your input Rebellious :D

I\'m updating the snippet right now.

 Respond  
Rebellious   -  Jan 03, 2008

It works very nicely, but just a little bug. It takes C into account when using $len. I would use $remove to remove character C from the output. =)

 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.