>>>>> "OC" == Owen Chavez <owen.chavez314...@gmail.com> writes:
OC> I'm writing a script for work that navigates users through a OC> complex decision-making tree, where decisions are made based on OC> some fairly in-depth processing of data entered by the user. The OC> script runs really well, does exactly what we need it to do. I'm OC> having some trouble with the humans using it making data entry OC> errors. A lot of these are caught by code I've included to prompt OC> the user to re-enter data when what they've entered doesn't make OC> sense (e.g., patient weight exceeding 250 kg., diastolic blood OC> pressure values that include non-numeric characters). Some errors OC> aren't caught by the script because they're not obvious, and it's OC> not being noticed by a human until several steps later. OC> What I want to do is be able to allow the user to return to a OC> prior step, but I'm having trouble figuring out how to make that OC> work using last or next, or a control loop. I could pry make OC> clever use of control loops in a smaller script, but we're talking OC> nearly 100 decisions in the tree. I never bothered to learn how OC> to use goto because it's deprecated (or on it's way?), but it OC> seems like this would be a good place to use it. At the same OC> time, I understand that use of goto is akin to sleeping with your OC> sister among most programmers in the Perl programming community. keep away from goto. period. perl is very flexible and can easily be coded to never need gotos. OC> I'd like to be able to share some code here, but the people who sign my OC> checks have asked me not to. my suggestion is to use subs. make each question into a sub call. if the call is generic enough you can use one sub for all the questions. you just pass it a hash and args that deal with the question. it could even be an object which holds all the data and a method. that isn't important. now you have to get into flow control at a higher level. this becomes what is known as a state machine (google for tons on this classic CS topic). a simple way to do this is to make each question into a hash. it contains the question, maybe some custom subs that check validity ( they can be anon subs or code refs to named subs). you can even have an array of them (stored in an anon array in the hash). you just put all the needed info about a question in a single hash. you make a few of these hashes to start and then write the driver code. when that is all working well, you can make all the rest of the question hashes. the next level is the driver code. this is passed a hash, it asks the question, takes in the answer, runs the verify subs and if any of them fails, it fails this sub call. the outer logic will then recall the same question until the user enters good data. the driver logic can either then go to the next question in the array of hashes or maybe elsewhere. the next location could be in the hash (don't use numbers or you will screw up when you insert a question later on) or in some outside list of ordering. there are many ways to handle ordering in a state machine. what is good about this design is its simplicity and extreme flexibility. the driver code can be modified to handle new variations and options in the hash. you can jump from question to question with different techniques. if you need to see some examples of table driven code, look into the t/ dir in File::Slurp. the recently released version has a TestDriver.pm module in t/ that does this very thing with an array of hashes. it just loops over each one, deals with the various possible options, runs code, checks results and errors and such. if all you want is a linear logic flow of the questions, this is all you need. anyhow, that should get started. and your bosses are poopy heads if they think basic question/answer code is so valuable. tell them i said this and will say it to their faces. this is basic stuff and no way possibly proprietary or a money making secret. uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/