On Tue, May 08, 2007 at 09:34:35PM -0400, Douglas Allan Tutty wrote:
> On Tue, May 08, 2007 at 01:22:10PM -0700, Bryan Irvine wrote:
>  
> > I need a fairly simple menu, and have thought about just simple
> > selects but figured now would also be a good time to learn something
> > new as well.  It's nothing so complex that I need to go ncurses to do.
> > Just a basic <option 1> then <option 3> then <run some command>
> > thing.
> 
> My front-ends I do in python.  It doesn't have a case/select.  I just
> use if/then/elif/....
> 
> Then there's Fortran with computed gotos; very slick.  I forget the
> syntax but is something like goto (10+choice)
>       11      ch1()
>               ...
>       12      ch2()
>               ...
>       13      ch3()
>               ...
> 
> It means that only one computation takes place instead of one comparison
> for each choice until one matches.

Just pointing out: if Python can do the job at all, you almost certainly
don't need that kind of micro-optimization in Fortran code. Also, this
is a menu. Efficiency is not exactly a big goal.

However, and this is where I go completely off-topic, while we're at it,
you don't need Fortran for this, most languages have equivalent
constructs (C):

switch(option) {
        case 1:
                ...
        case 2:
                ...
        case 3:
                ...
        default:
                /* error! */
                ...
}

or even

void (*dispatch[])(void) = {
        proc_opt1,
        proc_opt2,
        proc_opt3
}

void
proc_opt1(void)
{
        ...
}

void
proc_opt2(void)
{
        ...
}

void
proc_opt3(void)
{
        ...
}

In languages with higher order-functions, this can be written even more
concisely (Scheme):

(define dispatch
  (vector
    (lambda () ...)
    (lambda () ...)
    (lambda () ...)))

A suiteable make-menu macro could even make something like

(define toplevel-menu
  (make-menu
    ("opt1" (lambda () ...))
    ("opt2" (lambda () ...))
    ("another menu" another-menu)))

(define another-menu
  (make-menu
    ("opt3" (lambda () ...))
    ("opt4" (lambda () ...))
    ("top" toplevel-menu)))

do what it looks like it should do.

However, all of this is massively overkill. Just use a shell script.

                Joachim

-- 
TFMotD: mirroring-ports (7) - how to build a mirror for ports distfiles

Reply via email to