View Single Post
Old Nov 25th, 2006, 10:10 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by Darkhack View Post
This might help. You simply use an if-statement on your array.

http://www.ibiblio.org/g2swap/byteof...statement.html
BURN!!!! Hahahaha.

I'm sure that wasn't actually your question. You should probably specify what you mean by that. Are you talking about case-sensitive circumstances, or situations where there may be other garbage tagged along with it?

Or are you talking about how you could elegantly link each command to a different function call, without repetitive code?

I'd look into the idea of using a dictionary to map each command to a function call:
def output (*args):
    print args[0][0]

def list_of_files (*args):
    pass

def run_file (*args):
    pass

# parse the input in to a command and list of parameters
command = 'put'
params = ['"Text Here"']

# turn the command into a function pointer
commands = {"put":output, "dir":list_of_files, "start":run_file}
function = commands[command]

# call the function pointer with the parsed parameters
function (params)
Sane is offline   Reply With Quote