Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^4: General pattern for interactive text-mode script?

by Tanktalus (Canon)
on Jul 04, 2008 at 14:17 UTC ( #695570=note: print w/ replies, xml ) Need Help??


in reply to Re^3: General pattern for interactive text-mode script?
in thread General pattern for interactive text-mode script?

Minor nit, but I prefer not to use indices whenever possible. In C, they're basically required. That's fine. But you shouldn't need them in C++ or Perl where we have proper iterators.

So, instead of what you have, I'd combine them into a single list:

'Major task to perform' => [ { input => 'Add', next => 'task_1' }, { input => 'Delete', next => 'task_2' }, { input => 'Rename', next => 'task_3' }, ],
This should perform about as well as your method, but without the need for an index. As you loop through the hashrefs, once you find the input given, you already have the hashref with the details about whatever else you want. Further, you can very easily add additional fields if you find the need to. And if there is a long list of possible inputs, lining up the input with the associated task or next key or whatever will be trivial - not so with multiple array refs where you will have to manually ensure everything lines up even when you have 30 or 40 possible next states.

Or, if I was going to be case sensitive anyway (or I was willing to lc everything), set it up in a hash based on the inputs and be even faster:

'Major task to perform' => { 'add' => { next => 'task_1', help => 'Add something to the intern +al list' }, 'delete' => { next => 'task_2', help => 'Rename something (didn't ex +pect that, did you)' }, 'rename' => { next => 'task_3', help => 'Move something to a new loc +ation (didn't expect that, either, huh?)' }, },
When you get an input word, just lc it, and look it up for what the next state is. As you can see, I've gone and added extra info, which is trivial to do in hashes: if you get the 'help' word, you could loop through the hash (possibly by sorting on the key first, or maybe sorting based on a key in the value hash, e.g., "sort_order => $n"), printing the key and the help (Text::Table may help here). All the information is co-located. (In a larger system, it's arguable that the help should go with 'next' rather than 'this', but that's merely implementation details - I'm talking about a design that is more flexible and easier to maintain.)


Comment on Re^4: General pattern for interactive text-mode script?
Select or Download Code
Re^5: General pattern for interactive text-mode script?
by apl (Monsignor) on Jul 05, 2008 at 01:03 UTC
    Excellent suggestions. (I am, self-admittedly, a dinosaur. While I love Perl and like C++, I generally think in Fortran...)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-09-30 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (375 votes), past polls