gettok function

By Sonny on Jul 21, 2011

Usage:

gettok(items, number, delimiter)

e.g, gettok("apples|oranges", 1, "|") returns apples

e.g, gettok("apples|oranges", 2, "|") returns oranges

e.g, gettok("apples|oranges", 0, "|") returns 2

#Uncomment following line on Linux systems
##!/usr/bin/python

# Usage:
# gettok(items, number, delimiter)
# e.g, gettok("apples|oranges", 1, "|") returns apples
# e.g, gettok("apples|oranges", 2, "|") returns oranges
# e.g, gettok("apples|oranges", 0, "|") returns 2
def gettok(items, num, delim):
    tokens = 0
    i = 0
    while (i < len(items)):
        if (items[i] == delim):
            tokens += 1
        i += 1
    if (num == 0): return tokens+1
    i = 1
    items1 = 0
    pos = 0
    item = ""
    while (i <= tokens+1):
        if (items1 == 0): 
            cur = items[pos:]
            delpos = items.find(delim)
            items1 += 1
        elif (items1 > 0):
            pos = delpos+1
            cur = items[pos:]
            delpos += cur.find(delim)+1
        item = items[pos:delpos]
        if (i == tokens+1): item = cur 
        if (i == num):  return item
        i += 1

Comments

Sign in to comment.
PyThOn   -  Jul 27, 2011

ok, thanks

 Respond  
blackvenomm666   -  Jul 26, 2011

python this isnt for mirc so you prolly won't understand it

 Respond  
Sonny   -  Jul 26, 2011

It's used in python scripts to get a specific token within a string. It can be modified to get a specific token in any specific type, or multiple types. It can also be used to get the amount of tokens in the string, or if modified, the specific type.

 Respond  
PyThOn   -  Jul 26, 2011

Wtf is this? Lmfao
Never herd of it.

 Respond  
Sonny   -  Jul 26, 2011

Thanks, Aha2Y.

 Respond  
Sonny   -  Jul 26, 2011

Why dots?

 Respond  
PyThOn   -  Jul 22, 2011

....

 Respond  
Jordyk19   -  Jul 22, 2011

Usefull, Thanks. works good.

 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.