[comp.lang.icon] Case statement and string scanning

R.J.Hare@edinburgh.ac.uk (02/26/91)

 
I have a program in which I repetitively read in a string. According to what
the string contains, I do a variety of things - set up an option, display a
file, execute a system command, etc.
 
I would like to do this (fairly obviously) in a case statement, but the test I
am carrying out are so varied that I can't quite see how to fit them into a
case statement, for example, I will be doing:
 
if match("!",string) then...
if match(";",string) then...
if find("*",string) then...
if type(string) == "integer" then...
...some default action...
 
Any ideas on how I may set this up in a case statement please? Note that
because a string starts with "!", this does not preclude the possibility of it
also including a "*" elsewhere, so I guess a series of simple if statements
won't do the job as in such a circumstance, 2 of the blocks of code would be
executed...
 
I guess that there is a ridiculously simple answer, but I don't see it yet!
 
Thanks.
 
Roger Hare.

wgg@cs.washington.edu (William Griswold) (02/26/91)

To handle more general test/action alternatives, I would put the tests and
actions in procedures, store them in pairs in a list, and then iterate over
the tests in the list until success:

record pair(test,action)

...

procedure bangmatch(str)
    return match("!",str)
end
    
...

procedure main(arglist)

    case_list := [pair(bangmatch,bangaction),....]

    ...

    if (pr := !case_list).test(string) then 
	pr.action(string)

    ...
end


I'm sure there are more elegant solutions, but you get the idea.

					bill