in reply to World Builder: the recovery and archeology of old programs.

My first crack at this may be more comprehensible:

use Scalar::Util qw( looks_like_number ); use List::Util qw( first ); my @menu = ( 'Known star', 'Create a new star', 'List Known Stars', 'Q +uit' ); my $input = '...'; my $picked; if ( looks_like_number $input ) { $picked = $menu[ $input - 1 ]; } else { $picked = first { length $input <= length $_ and lc $input eq lc substr $_, 0, length $input; } @menu; } if ( !defined $picked ) { die "What do you mean by '$input'?"; }

Basically, if the input looks like a number, it's used as an index into the array of menu items. Otherwise, it loops through the menu looking for the first thing that "matches" the input.

My rewrite made this more useful as a menu in real code. Each menu item is now a hash ref that has a reference to a sub to handle the menu item. If need be, you can add more "stuff" to each hash ref. For example, maybe instead of substrings, you want to have regular expressions match the user's input. In that case, you could have a qr// stuck on each one used in the first loop instead of lc substr.

use Scalar::Util qw( looks_like_number ); use List::Util qw( first ); my @menu = ( { title => 'Known star', handler => \&load_star }, { title => 'Create a new star', handler => \&create_star }, { title => 'List Known Stars', handler => \&list_stars }, { title => 'Quit', handler => sub { exit } } ); my $input = '...'; my $picked; if ( looks_like_number $input ) { $picked = $menu[ $input - 1 ]; } else { $picked = first { length $input <= length $_->{title} and lc $input eq lc substr $_->{title}, 0, length $input; } @menu; } if ( !defined $picked ) { die "What do you mean by '$input'?"; } # "call" the menu item $picked->{handler}->();

Replies are listed 'Best First'.
Re^2: World Builder: the recovery and archeology of old programs.
by dwm042 (Priest) on Sep 17, 2008 at 16:51 UTC
    I wanted to thank you for your answer, kyle. I thought it interesting that you naturally gravitated to the use of a dispatch table in the menu, since the original Basic seems to be amenable to that approach. It doesn't hurt that Stephen Kimmel just about begins every functional block with a 'CLS' call.

    I'd show code, but until I get an answer from Ziff-Davis, (they own the Creative Computing IP) I'll have to just show bits and pieces, stuff I've written.

      This is a bit of an afterward to this thread.

      1. I contacted Ziff-Davis who didn't feel they had the copyright.

      2. I contacted Steven Kimmel, who asked me to give him a copy of the program to look at before giving permission to post.

      3. He has recently given me permission to post.

      4. I have posted the translated code on my web site.

Re^2: World Builder: the recovery and archeology of old programs.
by psini (Deacon) on Sep 17, 2008 at 16:27 UTC

    kyle, I think that looping through the array you should not take the first match, but also verify that it is the only match. This to enforce the "minimal unique" requirement: if there are two options beginning with "kn" then "kn" is not a valid input but perhaps "kno" is.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      If you want to be sure you have only one match, do it this way...

      my @picks = grep { ... } @menu; if ( 1 < scalar @picks ) { die "Too many options match your input, '$input'\n"; } $picked = $picks[0];