dwm042 has asked for the wisdom of the Perl Monks concerning the following question:

I've been losing sleep at nights as I've been trying to rewrite the program World Builder into Perl. There are lots of issues with this, such as 'why bother'? My answers are that (1) because I'm emotionally caught up in the project and I won't sleep until I finish, and (2) that posted listings have serious coding errors as a consequence of mindless scanning of the article. But other than that, the conversion of a 25 year old Basic program into something that resembles sane Perl creates some opportunities to examine interface issues.

The main menu looks something like this:

WORLD BUILDER 1. Known star 2. Create a new star 3. List Known Stars 4. Quit
Fair enough. I'd like to be able to type '1' here, or '2' here, but I'd also like to be able to type 'KNOWN' or 'Known', or 'known', or any minimal increment of 'known' that is unique and end up in the same place. This has to be a well understood problem, so I'm asking for help here.

Thanks in advance,

David.

Replies are listed 'Best First'.
Re: World Builder: the recovery and archeology of old programs.
by kyle (Abbot) on Sep 17, 2008 at 16:02 UTC

    My first crack at this may be more comprehensible:

    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}->();
      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.

      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];
Re: World Builder: the recovery and archeology of old programs.
by GrandFather (Saint) on Sep 17, 2008 at 21:07 UTC

    Why not provide a Tk (or other) GUI instead? It needen't be all that hard. Consider:

    use strict; use warnings; use Tk; use Tk::DialogBox; use Tk::Listbox; my $mw = MainWindow->new (); my %starData; my @params = ($mw, \%starData, ); $mw->Button (-text => 'Known star', -command => [\&knownStar, @params] +)->pack (); $mw->Button (-text => 'New star', -command => [\&newStar, @params])->p +ack (); $mw->Button (-text => 'List stars', -command => [\&listStars, @params] +)->pack (); $mw->Button (-text => 'Quit', -command => [\&quit, @params])->pack (); MainLoop (); sub knownStar { my ($mw, $starData) = @_; my @stars = map {[split ',']} split "\n", <<STARS; Sol, G2,1.0 Alpha Centuri A, G4,1.08 Alpha Centuri B, K1,.88 Epsilon Eridani, K2,.30 Tau Ceti, G8, .82 70 Ophiuchi A, K1,.9 70 Ophiuchi B, K5,.65 Eta Cassiopeiae A,f9,.94 Eta Cassiopeiae B,k6,.58 Sigma Draconis, G9,.82 36 Ophiuchi A, K2, .77 36 Ophiuchi B, K1,.76 Hr 7703, K2, .76 Delta Pavonis, G7,.98 82 Eridani, G5,.91 Beta Hydri, G1,1.23 Hr 8832, K3, .74 STARS my @names = map {$_->[0]} @stars; my $dlg = $mw->DialogBox ( -title => 'Select known star', -default_button => 'OK', -buttons => ['OK', 'Cancel'], ); my $listbox = $dlg->add ('Listbox', -listvariable => \@names, -selectmode => 'single' )->pack (); return if $dlg->Show () eq 'Cancel'; my @sel = $listbox->curselection (); return unless @sel; print "@{$stars[$sel[0]]}\n"; $starData->{$sel[0]} = $stars[$sel[0]]; } sub newStar { my ($mw, $starData) = @_; # Generate new star data here } sub listStars { my ($mw, $starData) = @_; } sub quit { my ($mw, $starData) = @_; # Save handling here if required exit; }

    Perl reduces RSI - it saves typing
Re: World Builder: the recovery and archeology of old programs.
by Skeeve (Parson) on Sep 17, 2008 at 15:46 UTC

    You could solve that with substrings and lc / uc. Just compare the entered string with the a substring from the menu options, starting at 0 and having the same length your entered string has. Convert both strings to lower (or upper) case before comparing.

    Or you could compare each menu string with /^$entered_string/i. But take care to quotemeta the entered string so that entering "." or ".*" doesn't confuse your program.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: World Builder: the recovery and archeology of old programs.
by planetscape (Chancellor) on Sep 17, 2008 at 19:32 UTC

    I am wondering if, perhaps, this page might offer a better original listing: USML / Planetary Stuff ?

    HTH,

    planetscape
      The answer is yes, it is a better original listing. They don't confuse numbers like '1' with the letter 'L'. L, in World Builder, is used for stellar luminosity.

      Update: Even this version has typos enough that it would not run accurately if not corrected.
Re: World Builder: the recovery and archeology of old programs.
by tod222 (Pilgrim) on Sep 17, 2008 at 18:44 UTC
    Ahh, Creative Computing, that brings back memories. It was a great magazine, I was sorry to see it fail.