RE: Case/switch
- Posted by Matthew Lewis <matthewwalkerlewis at YAHOO.COM> Feb 15, 2002
- 430 views
> -----Original Message----- > From: SR.Williamson [mailto:writeneu at hotmail.com] > Kat brought up case/switch. I'd love to see this become part of the > language. > > I was writing an artificial life program for Windows. > Basically, it was > a bunch of buttons in a grid. If a button was "hot", the > program would > check to see whether the button in the row above and to > either side was > "hot" to determine whether to make that button itself "hot". > > Well, as you can imagine, that works fine for the buttons in > the middle > of the grid. But when you get to the buttons in the grid > above, you have > to check to see if a button is at a window edge. So there are > a lot of > "if button1 = leftedge or button2 = rightedge" kind of stuff. It got > really tedious. The only think I could think was "This would > be so much > easier with a Case statement". > > Is there some easy way to do that that I'm missing? My typical method for eliminating large if/elsif (ie, case) structures is using routine_id. Then you can set up your condition[s] and the code to handle said conditions in an associative list. It's easy to add or delete cases this way, and you don't end up with a massive piece of code for which you can't easily follow the flow/decision process. Your example might look something like: sequence condition, result condition = { middle, leftedge, rightedge, topedge, bottomedge } result = { routine_id("do_middle"),.....} Then, for your decision: case = find( situation, condition ) call_proc( result[case] ) Matt Lewis